代码之家  ›  专栏  ›  技术社区  ›  Scott

为什么是ASP。网记得我这么健忘吗?

  •  2
  • Scott  · 技术社区  · 8 年前

    我有一个ASP。NET MVC 5项目使用ASP。NET Identity 2.2。我使用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))
                }
            });            
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Joel Coehoorn    8 年前

    20分钟是指空闲AppDomain回收所需的时间。如果您有一个不同的帐户连接到您的页面,为了防止页面闲置,它可能会记住您更长时间。