aspnet身份避免同时登录同一帐户

Thi*_*dio 7 asp.net-mvc-5 asp.net-identity asp.net-mvc-5.1 asp.net-identity-2

我正在搜索,但我找不到这个问题的一个答案:aspnet身份提供了一种避免同时从同一帐户登录的方法吗?

tra*_*max 10

标识不具有一个内置的方式来跟踪同时登录,但你可以做一个变通办法:每次用户登录,在,前设置身份验证cookie的,更改用户SecurityStamp通过await 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))
            }
        });            
Run Code Online (Sandbox Code Playgroud)

这样,每次用户登录时,所有其他会话都将失效,因为用户的SecurityStamp已更改.并且validateInterval足够低,因此其他auth-cookies可以很快失效.

  • @ThiagoCustodio`GetSecurityStampAsync`只会返回一个安全标记,这是一个存储在用户记录中的GUID - 它本身并没有做太多.至于避免第二次登录,它要复杂得多 - 你将以某种方式需要跟踪当前会话,为此保留ID,将该ID放入auth-cookie并在登录时检查当前会话是否与已存储的相同.我可以预见到这种方法存在的一系列问题.最终,如果您希望用户停止共享登录,建议的方法很快就会使密码共享很糟糕. (2认同)