Project 在 .NET Core 2.2 中并使用 Identity。 当用户登录并且会话超时时,他会被控制器中的属性[Authorize]
重定向到登录页面。不幸的是,用户被重定向到/Identity/Account/Login而不是/Account/Login,并且设计不同。我需要重定向到/帐户/登录。我错过了什么?
我在配置服务中有以下内容:
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
});
我想到了选择。登录路径是要走的路...
如果您想按ConfigureApplicationCookie
设置,则需要使用AddIdentity
而不是AddDefaultIdentity
。
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);
});
我遇到了类似的问题,唯一解决它的方法是使用创建PathString
而不是对任何路径使用字符串。
options.LoginPath = new PathString("/Home/Index");
就我而言,我必须显式指定 DefaultAuthenticateScheme。
.AddAuthentication (options => {
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie (CookieAuthenticationDefaults.AuthenticationScheme, options => {
options.LoginPath = "/login";
// ....
});
而不是
.AddAuthentication (CookieAuthenticationDefaults.AuthenticationScheme)