2015-11-04 59 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