如何强制用户退出 MVC5 中的锁定
How to force user sign out on lockout in MVC5
我正在将 .Net MVC5 与 Identity 结合使用,一般来说我是开发新手,所以请多多包涵。
如何显示锁定页面并根据锁定状态强制用户注销?
出于测试目的,我创建了两个用户,一个角色 "Admin",另一个角色 "RegisteredUser"。
我做了一个 Ajax 操作 enable/disables 并将锁定设置为 DateTime.MaxValue 只是为了测试特定 "RegisteredUser"。它连接在一些仅供管理员使用的视图中。
如果某些 "RegisteredUser" 在登录时被 "Admin" 锁定,我想在他的下一个请求或至少 60 分钟后显示 "RegisteredUser" 锁定页面。
现在我听说了 AuthorizationFilter 和 ActionFilter 或者 Global.asax 中的一些事件处理程序。但也许已经有某种机制可以禁用用户并立即反映在目标用户角色上?
如何最好地解决 locking/disabling 用户的这个问题并立即执行。
我会使用实现 OnActionExecuting. At that point you can check if the user needs to be redirected 锁定屏幕的基础控制器。
在锁定用户的同时,我还更改了 SecurityStamp,这将导致身份验证 cookie 失效。可以在 Startup.Auth 中找到的 Integrated Identity 2.0 机制每隔(默认值:30 分钟)检查一次 cookie 完整性,如果无效(例如,cookie 具有不同的 SecurityStamp),则将用户注销。检查的时间延迟可以更改,但我认为它适合我的项目。
这部分代码使一些用户的 cookie 无效,我安排了一些每天的检查,如果为真,就会触发它;
var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = userManager.FindByName(username);
userManager.SetLockoutEnabled(user.Id, true);
userManager.SetLockoutEndDate(user.Id, DateTime.MaxValue);
user.SecurityStamp = Guid.NewGuid().ToString("D");
userManager.UpdateSecurityStamp(user.Id);
负责 cookie 验证 and/or 重新生成的部分已经存在,但如果我没记错的话,它只在 Identity 2.0 中存在。它的配置在 Startup.Auth.cs 文件中,看起来像这样:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
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(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
我在 Whosebug 上的不同文章中找到了所有这些,所以感谢那些人。
Kevin Raffay 的答案肯定有效,所以我将其标记为答案,因为我是新手,我不确定哪种方法更好。
我正在将 .Net MVC5 与 Identity 结合使用,一般来说我是开发新手,所以请多多包涵。
如何显示锁定页面并根据锁定状态强制用户注销?
出于测试目的,我创建了两个用户,一个角色 "Admin",另一个角色 "RegisteredUser"。
我做了一个 Ajax 操作 enable/disables 并将锁定设置为 DateTime.MaxValue 只是为了测试特定 "RegisteredUser"。它连接在一些仅供管理员使用的视图中。
如果某些 "RegisteredUser" 在登录时被 "Admin" 锁定,我想在他的下一个请求或至少 60 分钟后显示 "RegisteredUser" 锁定页面。
现在我听说了 AuthorizationFilter 和 ActionFilter 或者 Global.asax 中的一些事件处理程序。但也许已经有某种机制可以禁用用户并立即反映在目标用户角色上?
如何最好地解决 locking/disabling 用户的这个问题并立即执行。
我会使用实现 OnActionExecuting. At that point you can check if the user needs to be redirected 锁定屏幕的基础控制器。
在锁定用户的同时,我还更改了 SecurityStamp,这将导致身份验证 cookie 失效。可以在 Startup.Auth 中找到的 Integrated Identity 2.0 机制每隔(默认值:30 分钟)检查一次 cookie 完整性,如果无效(例如,cookie 具有不同的 SecurityStamp),则将用户注销。检查的时间延迟可以更改,但我认为它适合我的项目。
这部分代码使一些用户的 cookie 无效,我安排了一些每天的检查,如果为真,就会触发它;
var userManager = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = userManager.FindByName(username);
userManager.SetLockoutEnabled(user.Id, true);
userManager.SetLockoutEndDate(user.Id, DateTime.MaxValue);
user.SecurityStamp = Guid.NewGuid().ToString("D");
userManager.UpdateSecurityStamp(user.Id);
负责 cookie 验证 and/or 重新生成的部分已经存在,但如果我没记错的话,它只在 Identity 2.0 中存在。它的配置在 Startup.Auth.cs 文件中,看起来像这样:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
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(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
我在 Whosebug 上的不同文章中找到了所有这些,所以感谢那些人。
Kevin Raffay 的答案肯定有效,所以我将其标记为答案,因为我是新手,我不确定哪种方法更好。