Asp.net-Mvc-5

aspnet 身份避免同時登錄同一個帳戶

  • January 4, 2015

我正在搜尋,但找不到這個問題的答案:aspnet 身份是否提供了一種避免從同一帳戶同時登錄的方法?

Identity 沒有內置的方法來跟踪同時登錄,但您可以做一個解決方法:每次使用者登錄時,在設置 auth-cookie 之前,更改使用者SecurityStampawait userManager.UpdateSecurityStampAsync(user.Id);

並確保你有這部分Startup.Auth.cs

       app.UseCookieAuthentication(new CookieAuthenticationOptions
       {
           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(5),
                   regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
           }
       });            

這樣每次使用者登錄時,所有其他會話都將失效,因為使用者上的 SecurityStamp 已更改。並且validateInterval值足夠低,因此其他身份驗證cookie可以很快失效。

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