2017-07-09 71 views
2

我正在使用asp.net標識的asp.net mvc應用程序。
Startup.Auth.cs文件中,我將ExpireTimeSpan設置爲20天,但當我登錄到我的應用程序時,我的應用程序早於20天就被註銷,我必須登錄agian!爲什麼要以比asp.net身份的ExpireTimeSpan更早註銷?

Startup.Auth.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
    LoginPath = new PathString("/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, User>(
      validateInterval: TimeSpan.FromMinutes(0), 
      regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) 
    }, 
    ExpireTimeSpan = TimeSpan.FromDays(20), 
    SlidingExpiration = true 
}); 

而在Login行動:

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true); 

更新
當我登錄,我.AspNet.ApplicationCookie並且它的過期日期被設置爲「20」天后,並且當我第二天打開網站時,我是註銷,但是Cookie存在。

enter image description here

是什麼這個問題的原因是什麼?
在此先感謝。

+0

將'model.RememberMe'設置爲'true',並確定在調用PasswordSignInAsync時它是'true'? – CodingYoshi

+0

提前多久? – sepehr

+0

@CodingYoshi是的,'RememberMe'設置爲'true',我敢肯定! –

回答

1

下面是退出早於預期的幾個原因:

  • 有同一域和所有的人都the same cookie name(cookie名稱衝突)多個web應用程序。在這種情況下,應用A會覆蓋應用B的Cookie。

  • validateInterval設爲零/ TimeSpan.FromMinutes(0),以UpdateSecurityStamp所有呼叫都將強制用戶退出,然後立即重新登錄,包括UserManager.CreateAsyncUserManager.RemovePasswordAsyncUserManager.UpdatePasswordUserManager.RemoveLoginAsyncUserManager.ChangePhoneNumberAsync/SetPhoneNumberAsyncUserManager.SetTwoFactorEnabledAsyncUserManager.SetEmailAsync。這意味着如果更新用戶的屬性,將會調用UpdateSecurityStamp

  • 如果更新服務器上的.NET framework,它也會覆蓋machine-key。如果更改該標記,則會將所有已發出的Cookie標記爲無效。機器密鑰是一組用於加密和解密cookie的密鑰。如果您在負載均衡器後面運行,則需要確保Web場使用一致的machine-key
  • 如果您存儲的Cookie數量過多user-claims,它們會變大(大於〜5K),有些瀏覽器會拒絕它們。所以請查看發佈的cookie的大小。
  • 用戶可以將其瀏覽器設置爲在關閉cookie時進行刪除(隱私瀏覽)。