我看到您正在使用ASP.NET Identity 2.您正在尝试执行的操作已经内置。您只需更改SecurityStamp并且所有以前的身份验证Cookie不再有效。
后更改密码,您还需要更改SecurityStamp:
await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
await UserManager.UpdateSecurityStampAsync(User.Identity.GetUserId());
如果你希望用户保持登录状态,则必须重新发出一个新的身份验证cookie(登入):
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
否则,启动密码更改的用户/会话也将被注销。
,并立即退出其他所有会话,你需要降低在config检查的间隔时间:
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.FromSeconds(1),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
重现步骤:
- 创建一个新的Asp.Net Web应用程序在VS2015 。
- 选择MVC模板。
- 编辑App_Stat/Startup.Auth.cs 34行:变化
validateInterval: TimeSpan.FromMinutes(30)
到validateInterval: TimeSpan.FromSeconds(1)
- 编辑控制器/ ManageController.cs,管线236:添加
UserManager.UpdateSecurityStampAsync
方法调用。
- 运行项目,创建用户,登录,打开不同的浏览器并登录。
- 更改密码,刷新其他浏览器中的页面:您应该注销。
你似乎粘贴了两次相同的图像。另外,我是否错误地认为你的方法只关闭了一个打开的会话,但是如果有更多的会话打开(两个,三个),则会失败 - 第一个打开的会话将重新进行身份验证,但其余的仍然无需身份验证即可运行。 –
@Wiktor谢谢,我更新了图像并添加了一些评论。 –
此方法性能高效 – anand