锁定用户时,我也更改了SecurityStamp,这将导致auth cookie失效。可以在Startup.Auth中找到Integrated Identity 2.0机制,每次(默认:30分钟)检查cookie的完整性,如果无效则退出用户(例如,具有不同SecurityStamp的cookie)。检查的时间延迟可以改变,但我认为它适合我的项目。
这部分代码会使某些用户的cookie无效,并且我会每天安排一些检查,如果检测结果为true,将会触发它;
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验证和/或重新生成的部分已经存在,但如果我没有弄错,它只能从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))
}
});
我发现这一切都在这里就各条款的计算器如此归功于那些人。
Kevin Raffay的答案肯定是有效的,所以我会将它标记为答案,因为我对此很陌生,我不确定哪种方法更好。
嗯....这当然是一种方法,但我走了另一个解决方案,因为我不想经常检查数据库。 – Dino