ucn*_*obi 6 c# asp.net-mvc asp.net-identity
我想要做的是将用户ID限制为只能一次登录到一个设备.例如,用户ID"abc"登录到他们的计算机.用户ID"abc"现在尝试从手机登录.我想要发生的是在他们的计算机上杀死会话.
我正在使用Asp.net mvc身份会员资格并使用SecurityStamp来实现此目的.这是我在帐户/登录操作中的代码:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var user = UserManager.FindByEmail(model.Email);
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
await UserManager.UpdateSecurityStampAsync(user.Id);
Run Code Online (Sandbox Code Playgroud)
根据UpdateSecurityStampAsyncdoc所说的方法:为用户生成一个新的安全标记,用于SignOutEverywhere功能.但它不起作用.
如果您想在其他设备上启用 cookie 的即时失效,那么每个请求都必须访问数据库以验证 cookie。为此,您需要在 Auth.Config.cs 中配置 cookie 失效并将 validateInterval 设置为 0:
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<UserManager, ApplicationUser>(
validateInterval: TimeSpan.FromSeconds(0),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2837 次 |
| 最近记录: |