Asp.net mvc 5 aspnet标识避免同时登录同一帐户
我正在搜索,但找不到这个问题的答案:aspnet identity是否提供了一种避免从同一帐户同时登录的方法?identity没有内置的方法来跟踪同时登录,但您可以做一个变通:每次用户登录时,在设置auth cookie之前,通过wait userManager.UpdateSecurityStampAsync(user.Id)更改用户的Asp.net mvc 5 aspnet标识避免同时登录同一帐户,asp.net-mvc-5,asp.net-identity-2,asp.net-mvc-5.1,asp.net-identity,Asp.net Mvc 5,Asp.net Identity 2,Asp.net Mvc 5.1,Asp.net Identity,我正在搜索,但找不到这个问题的答案:aspnet identity是否提供了一种避免从同一帐户同时登录的方法?identity没有内置的方法来跟踪同时登录,但您可以做一个变通:每次用户登录时,在设置auth cookie之前,通过wait userManager.UpdateSecurityStampAsync(user.Id)更改用户的SecurityStamp 并确保在Startup.Auth.cs中包含此部分: app.UseCookieAuthentication(new
SecurityStamp
代码>
并确保在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))
}
});
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(5),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});
这样,每次用户登录时,所有其他会话都将无效,因为用户上的SecurityStamp已更改。将
validateInterval
设置为足够低的值,以便其他身份验证cookie可以很快失效。如果该用户存在活动会话,我正在寻找避免登录的方法。UserManager.GetSecurityStampAsync可以做到吗?@ThiagoCustodioGetSecurityStampAsync
将只返回一个安全戳,它是存储在用户记录中的GUID,但它本身没有多大作用。至于避免第二次登录,这要复杂得多——您将需要以某种方式跟踪当前会话,保留该会话的ID,将该ID放入auth cookie中,并在登录时检查当前会话是否与已存储的会话相同。我可以预见这种方法会有一大堆问题。最终,如果您想让用户停止共享登录名,建议的方法会很快让他们知道密码共享不好。wait userManager.UpdateSecurityStampAsync(user.Id),MVC 5中的哪个文件?@trailmax有完整的示例或步骤吗?@Pravin