这是一个常见的请求或建议,即Web应用程序不允许用户一次激活多个会话。 换句话说,在用户登录应用程序后,不应该允许他打开不同类型的浏览器(或使用另一台计算机)再次登录,直到他的第一个会话结束。 如果用户尝试再次登录,则应用程序应使之前的会话过期。 我试图实现这一壮举无济于事。 任何帮助或 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我试图限制用户登录Web应用程序。 一些来源[ 1 ]和[ 2 ]使用await _userManager.UpdateSecurityStampAsync (loginUser);
来使用相同的想法await _userManager.UpdateSecurityStampAsync (loginUser);
这是AccountController中完整的Login操作代码:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login (LoginViewModel model, string returnUrl = null) {
ViewData["ReturnUrl"] = returnUrl;
if ( ModelState.IsValid ) {
var loginUser = await _userManager.FindByEmailAsync(model.Email);
if ( loginUser == null ) {
ModelState.AddModelError (string.Empty, "Invalid username/password.");
return View ();
}
await _userManager.UpdateSecurityStampAsync (loginUser);
var result = await _signInManager.PasswordSignInAsync(loginUser, model.Password, isPersistent: false, lockoutOnFailure: true);
if ( result.Succeeded ) {
_logger.LogInformation ("User logged in.");
return RedirectToLocal (returnUrl);
}
if ( result.RequiresTwoFactor ) {
return RedirectToAction (nameof (LoginWith2fa), new { returnUrl, model.RememberMe });
}
if ( result.IsLockedOut ) {
_logger.LogWarning ("User account locked out.");
return RedirectToAction (nameof (Lockout));
} else {
ModelState.AddModelError (string.Empty, "Invalid login attempt.");
return View (model);
}
}
// If we got this far, something failed, redisplay form
return View (model);
}
以上登录操作看起来如此熟悉。 它只是通过添加UpdateSecurityStampAsync来修改,以便每次用户在PasswordSignInAsync之前登录时更新表中的'concurrencyStamp'和'SecurityStamp'列(dbo.AspNetUsers)。
我尝试从2个不同的浏览器(第一次和第二次尝试从Chrome;第三次和第四次尝试来自firefox)进行屏幕拍摄,同样登录。 执行UpdateSecurityStampAsync后捕获第2和第4个。 第二和第三个具有相同的ConcurrencyStamp和SecurityStamp 。
我还修改了ConfigureServices方法中的start.cs:
services.Configure<SecurityStampValidatorOptions> (option =>
option.ValidationInterval = TimeSpan.FromSeconds (0)
);
services.AddAuthentication ()
.Services.ConfigureApplicationCookie (options => {
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes (30);
});
但是,在我修改上面的代码之后,我仍然可以同时从其他Web浏览器登录。 我也可以使用相同的用户名和密码成功登录其他机器。
任何帮助/建议都非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.