.NET Core:如何发送到/ Account / Login而不是/ Identity / Account / Login

时间:2019-12-09 01:29:17

标签: asp.net asp.net-core asp.net-identity

项目位于.NET Core 2.2中,并使用Identity。 当用户登录且会话超时时,控制器中的属性[Authorize]将其重定向到登录页面。不幸的是,用户被重定向到/ Identity / Account / Login而不是/ Account / Login,并且设计有所不同。我需要重定向到/ Account / Login。我想念什么?

ConfigureServices中包含以下内容:

    services.Configure<IdentityOptions>(options =>
    {
        // Password settings
        options.Password.RequireDigit = true;
        options.Password.RequiredLength = 8;
        options.Password.RequireNonAlphanumeric = false;
        options.Password.RequireUppercase = true;
        options.Password.RequireLowercase = false;
        options.Password.RequiredUniqueChars = 6;

        // Lockout settings
        options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
        options.Lockout.MaxFailedAccessAttempts = 4;
        options.Lockout.AllowedForNewUsers = true;

        // User settings
        options.User.RequireUniqueEmail = true;
    });

    services.ConfigureApplicationCookie(options =>
    {
        // Cookie settings
        //options.Cookie.HttpOnly = true;
        options.Cookie.Expiration = TimeSpan.FromMinutes(20);
        options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
        options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
        options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
        options.SlidingExpiration = true;
        options.ExpireTimeSpan = TimeSpan.FromMinutes(20);      // 20 minutes logged in session timeout
    });

我认为有选择。LoginPath是必经之路...

2 个答案:

答案 0 :(得分:1)

如果您想用AddIdentity进行设置,则需要使用AddDefaultIdentity而不是ConfigureApplicationCookie

services.AddIdentity<IdentityUser,IdentityRole>()
            //.AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

或者您可以尝试配置CookieAuthenticationOptions来满足您的要求。

services.AddIdentity<IdentityUser,IdentityRole>()
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

services.PostConfigure<CookieAuthenticationOptions>(IdentityConstants.ApplicationScheme,
options =>
{

    options.Cookie.Expiration = TimeSpan.FromMinutes(20);
    options.LoginPath = "/Account/Login"; 
    options.LogoutPath = "/Account/Logout";
    options.AccessDeniedPath = "/Account/AccessDenied"; 
    options.SlidingExpiration = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(20);  

});

答案 1 :(得分:0)

我有一个类似的问题,解决此问题的唯一方法是使用创建PathString而不是在任何路径中使用字符串。

options.LoginPath = new PathString("/Home/Index");