2015-11-04 62 views
1

所以我有一个MVC5的Web应用程序,我使用内置的ASP.Identity框架进行身份验证。我想让我的用户保持30天的登录状态。ASP身份CookieAuthentication不能正常工作

这里是我的代码在Startup.Auth.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
       CookieName = "Test", 
       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.FromDays(30), 
         regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
       }, 
       ExpireTimeSpan = TimeSpan.FromDays(30) 
      });  

我检查的cookie登录后,它是真实的,过期是30天,但几分钟后,/当我试图再次访问该网站时,登录状态消失。任何想法为什么?

+0

用户在数据库的'SecurityStamp'字段中是否有任何值? – trailmax

回答

1

看起来OnValidateIdentity有一个bug。无论何时尝试重新生成cookie,即使原始Cookie持久存在,它也会将IsPersistent设置为false

如果我没有错,您可能会在关闭并重新打开浏览器后面临此问题。可以通过添加IsPersistent声明来解决此问题,请尝试查看此link