Asp.net

ASP.NET Identity AuthenticationManager 與 SignInManager 和 cookie 過期

  • October 15, 2014

使用 AuthenticationManager SignIn 與使用 SignInManager PasswordSignIn/SignIn 有什麼區別?我有一個使用 SignInManager 的實現,並將我的 cookie 過期設置為 30 天,但似乎我的 web 應用程序會在 30 天之前隨機過期我的 cookie。使用 SignInManager 實現會導致這種情況嗎?我應該改用 AuthenticationManager 實現嗎?

開箱即用的範常式式碼顯示了這樣的登錄,但我也看到了其他使用 AuthenticationManager 實現的範例。

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

這是我的啟動配置。

           app.UseCookieAuthentication(new CookieAuthenticationOptions
       {
           AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
           ExpireTimeSpan = TimeSpan.FromDays(30),
           LoginPath = new PathString("/signin"),
           Provider = new CookieAuthenticationProvider
           {
               OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<AppUserManager, AppUser>(
                   validateInterval: TimeSpan.FromMinutes(30),
                   regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
           }
       });
       app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
       app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
       app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

在身份框架版本 2.1.0 發布之前,我們必須編寫自己的程式碼以獲得雙重身份驗證、帳戶鎖定、EmailToBeConfirmed 等的結果(SignInStatus)。使用 SignInManager,這已被簡化,我們得到 SignInStatus一行程式碼。

您可以在 NuGet 包和編譯兩個版本之後理解此檢查。

2.0.0 版:Install-Package Microsoft.AspNet.Identity.Samples -Version 2.0.0-beta1 -Pre

2.1.0 版:Install-Package Microsoft.AspNet.Identity.Samples -Pre

AuthenticationManager.SignIn是用於完成使用者登錄過程的機制,因此和 SignInManager之間沒有任何區別。我們可以將其解釋為一個幫助類來管理所有類型的身份驗證,例如/ 、.AuthenticationManager.SignIn``SignInManager.PasswordSignIn/SignIn``SignInManager``PasswordSignIn``SignIn``SignInOrTwoFactor

因此,cookie 的到期不取決於您用於登錄的方法,因為所有這些都在CookieAuthenticationOptions啟動時配置。

引用自:https://stackoverflow.com/questions/26373614