用户总是需要在 20 - 30 分钟后登录
User always required to login after 20 - 30 minutes
无论我尝试过什么,我似乎都无法将登录之间所需的时间增加超过 20 ~ 30 分钟。即使用户持续处于活动状态也是如此 - 所以这不仅仅是 "idle" 时间,它是登录之间的最长时间。
20 分钟后,用户将被重定向到登录页面以再次登录。
奇怪的是,当我将 CookieOptions.ExpireTimeSpan 减少到大约 10 秒时,它会在 10 秒后按预期重定向。我还测试了 SlidingExpiration 并且它也有效 - 使用较低的 ExpireTimeSpan,任何大于 20/30 分钟的时间似乎都不起作用。
我认为这可能与我的主机服务器的应用程序池设置有关。也许应用程序池在 20 分钟后死亡导致登录无效,但即使我 运行 应用程序直接来自 Visual Studio.
也会发生这种情况
老实说,我没有想法,如果这是一个 cookie 问题,我什至不知道如何调试它。即使有一种方法可以在登录后读取 cookie 上的 ExpireTimeSpan 值,我也非常感谢任何帮助。
这是我目前在启动时的代码:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
//services.AddSingletons, Transients etc..
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
// Lockout settings.
// User settings.
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Login";
//sliding expiration doesn't seem to work, nor does expiretimespan
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromHours(1);
});
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
config.Filters.Add(new AuthorizeFilter(policy));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddHttpContextAccessor();
}
我的配置:
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我用这个登录:
[HttpPost]
[Route("/Login")]
[AllowAnonymous]
public async Task<ActionResult> Login(string returnUrl, LoginFormModel loginFormModel)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid) {
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(loginFormModel.Username, loginFormModel.Password, loginFormModel.RememberMe, lockoutOnFailure: true);
if (result.Succeeded) {
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
}
}
非常感谢任何提示!谢谢!
查看类似问题 and the bug report here。
简而言之,这是 ASP.NET Core 2.1 中的一个错误,如果您正在实施自己的 IUserStore 但未实施 IUserSecurityStampStore,即使 cookie 没有过期,用户也会在 30 分钟后注销。
快速解决方法是将安全标记验证增加到 safe-enough 值,如下所示:
services.Configure<SecurityStampValidatorOptions>(o => o.ValidationInterval = TimeSpan.FromHours(10));
无论我尝试过什么,我似乎都无法将登录之间所需的时间增加超过 20 ~ 30 分钟。即使用户持续处于活动状态也是如此 - 所以这不仅仅是 "idle" 时间,它是登录之间的最长时间。 20 分钟后,用户将被重定向到登录页面以再次登录。
奇怪的是,当我将 CookieOptions.ExpireTimeSpan 减少到大约 10 秒时,它会在 10 秒后按预期重定向。我还测试了 SlidingExpiration 并且它也有效 - 使用较低的 ExpireTimeSpan,任何大于 20/30 分钟的时间似乎都不起作用。
我认为这可能与我的主机服务器的应用程序池设置有关。也许应用程序池在 20 分钟后死亡导致登录无效,但即使我 运行 应用程序直接来自 Visual Studio.
也会发生这种情况老实说,我没有想法,如果这是一个 cookie 问题,我什至不知道如何调试它。即使有一种方法可以在登录后读取 cookie 上的 ExpireTimeSpan 值,我也非常感谢任何帮助。
这是我目前在启动时的代码:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
//services.AddSingletons, Transients etc..
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
// Lockout settings.
// User settings.
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Login";
//sliding expiration doesn't seem to work, nor does expiretimespan
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromHours(1);
});
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
config.Filters.Add(new AuthorizeFilter(policy));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddHttpContextAccessor();
}
我的配置:
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我用这个登录:
[HttpPost]
[Route("/Login")]
[AllowAnonymous]
public async Task<ActionResult> Login(string returnUrl, LoginFormModel loginFormModel)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid) {
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result = await _signInManager.PasswordSignInAsync(loginFormModel.Username, loginFormModel.Password, loginFormModel.RememberMe, lockoutOnFailure: true);
if (result.Succeeded) {
_logger.LogInformation("User logged in.");
return LocalRedirect(returnUrl);
}
}
}
非常感谢任何提示!谢谢!
查看类似问题
简而言之,这是 ASP.NET Core 2.1 中的一个错误,如果您正在实施自己的 IUserStore 但未实施 IUserSecurityStampStore,即使 cookie 没有过期,用户也会在 30 分钟后注销。
快速解决方法是将安全标记验证增加到 safe-enough 值,如下所示:
services.Configure<SecurityStampValidatorOptions>(o => o.ValidationInterval = TimeSpan.FromHours(10));