我的身份认证设置似乎超出了.AspNetCore.Identity.Application cookie,它超出了浏览器会话或其他时间(大约30分钟)。我可以将应用程序Cookie的过期时间设置为较短的时间(如1分钟),这将按预期工作,但是当我尝试10小时的操作时,其他操作会迫使用户退出30分钟。我不知道是哪个部分导致注销。 通过选中“记住我”将IsPersistant设置为true。我可以在浏览器中看到Cookie,并且expires / Max-Age设置为10小时,并且当我被踢到登录名时也不会消失。
看着这个(以及许多类似的问题) ASP.NET Core MVC: setting expiration of identity cookie
将此用于ajax操作 Handling session timeout with Ajax in .NET Core MVC
Startup.cs
services.AddIdentity<EntityFramework.ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
//password options
options.Password.RequiredLength = 8;
options.Password.RequiredUniqueChars = 3;
//Lockout options
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
});
services.ConfigureApplicationCookie(options =>
{
//timeout span
options.Cookie.Expiration = TimeSpan.FromHours(10);
options.ExpireTimeSpan = TimeSpan.FromHours(10);
options.Cookie.MaxAge = TimeSpan.FromHours(10);
options.LoginPath = "/Account/Login";
//https://stackoverflow.com/questions/55344665/handling-session-timeout-with-ajax-in-net-core-mvc
options.Events.OnRedirectToLogin = (context) =>
{
//identify if Ajax (json request)
if (context.Request.ContentType != null && context.Request.ContentType.Contains( "application/json"))
{
context.HttpContext.Response.StatusCode = 401;
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.CompletedTask;
};
});
services.AddAuthentication(options =>
{
options.DefaultScheme = IdentityConstants.ApplicationScheme;
})
在我的登录操作中
await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);