【发布时间】:2018-07-06 23:52:19
【问题描述】:
我有一个使用 ASP.NET Identity 2.2 的 ASP.NET MVC 5 项目。我使用 Visual Studio 中的“个人用户帐户”身份验证选项进行了设置。我可以毫无问题地登录。如果我在登录后没有单击记住我并关闭浏览器,我的凭据在重新打开时不会被记住。如果我在登录后单击记住我并关闭浏览器,我的凭据会在重新打开时被记住。到目前为止一切顺利。
当我单击“记住我”,登录,关闭浏览器,然后长时间关闭时出现问题(我的测试让我认为它必须超过 20 分钟)。当我这样做时,我的凭据被忘记了,我必须再次登录。为什么会出现这种情况?
当我在 20 分钟后再次打开浏览器时,AspNet.ApplicationCookie cookie 仍然存在,并且过期时间大约在两周后。
我看到其他文章提到了 UseCookieAuthentication 方法调用,所以我将其包含在下面。我相信我为此使用了默认值。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
【问题讨论】:
-
20 分钟大约是闲置的 AppDomain 回收所需的时间。如果您有其他帐户连接到您的信息页,为了防止它闲置,它可能会记住您更长时间。
-
我的虚拟主机提供商不允许我修改 AppDomain 空闲超时,所以我设置了一个计划任务来定期 ping 网站。这样做之后,我不再看到这个问题。随意添加答案,我会将其标记为已接受。
标签: c# asp.net asp.net-mvc cookies asp.net-identity