更改密码后,从所有浏览器注销用户 [英] Logout User From all Browser When Password is changed

查看:103
本文介绍了更改密码后,从所有浏览器注销用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重置密码"页面:

I have a Reset Password page:

当用户填写详细信息并单击Reset Password按钮时.以下控制器称为:

When the user fills the details and clicks the Reset Password button. The following controller is called:

public ActionResult ResetPassword(ResetPassword model)
{
    ...
    return RedirectToAction("Logout");
}

当用户更改密码时,他们从浏览器获得Logged Out.但是,如果它们同时登录到另一个浏览器,则它们仍保持在另一个浏览器上的登录.

When the user changes their password, they get Logged Out from the browser. However, if they are logged into another browser at the same time they remain logged in on the other browser.

我要在用户更改密码后从所有登录的浏览器中注销该用户.

I want to log out the user from all browsers they are logged into when they change their password.

推荐答案

所以我回到家,决定整理一些代码.给我看代码!!!

So I got home and decided to put together some code. Show me the code !!!

我会使用处理程序,因此验证总是在用户首次访问应用程序时完成,并且每次操作方法访问都在一个地方完成.

I would use a handler so the verification is always done when the user first access the application and it is done at one place for every action method access.

这个想法是,当用户重设密码时,应用程序会记录该用户已重设密码,并且首次未登录并注销该用户.

The idea is when the user reset their password, the application records the user has reset their password and have not logged in for the first time and sign out the user.

user.HasResetPassword = true;
user.IsFirstLoginAfterPasswordReset = false;

当用户登录时,应用程序将验证用户先前是否重置了密码,并且现在是第一次登录.如果这些语句有效,则应用程序将更新其记录,以表明您尚未重置密码,并且您也没有首次登录.

When the user signs in, the application verifies if the user had previously reset their password and is now signing in for the first time. If these statements are valid the application updates its records to say you have not reset your password and you are not signing in for the first time.

第1步

向ApplicationUser模型添加两个属性

Add two properties to ApplicationUser model

第2步

在Models文件夹中添加具有以下实现的类AuthHandler.cs. 在此阶段,您将验证用户是否已重设密码,并且自重设密码以来首次未登录.如果是这样,请将用户重定向到登录名.

Add a class AuthHandler.cs in Models folder with the implementation below. At this stage you verify if the user has reset their password and has not logged in for the first time since the password was reset. If this is true, redirect the user to the login.

第3步

在RouteConfig.cs中调用AuthHandler,以便为您的应用程序的每个传入的HTTP请求调用它.

In RouteConfig.cs call the AuthHandler so that it is invoked for each incoming http request to your application.

第4步

在ResetPassword方法中,添加以下实现.在此步骤中,当用户重设密码时,将属性更新为,即他们重设了密码并且没有首次登录.请注意,重设密码时,用户也会被明确注销.

In ResetPassword method add implementation as below. At this step when a user has reset their password update the properties to say , they have reset their password and have not logged in for the first time. Notice the user is also signed out explicitly when they reset their password.

第5步

在Login方法中,添加以下实现.在此步骤中,如果用户成功登录,请验证其密码是否已重置并且他们首次登录为假.如果满足所有条件,请更新数据库中的属性,以便这些属性处于准备状态,以备将来用户重置密码时使用.这样的循环确定并更新了密码重置状态,并在重置密码后首次登录.

In Login method add the implementation below. At this step if a user logins in successfully, verify their password was reset and they has logged for the first time is false. If all the conditions are true, update the properties in the database, so the properties are in a state ready for when the user resets the password in the future. So kind of a loop determining and updating the state of the password reset and first logins after resetting the password.

最后

您的AspnetUsers表应如下所示

Your AspnetUsers table should look as below

评论

这就是我的处理方式.我尚未对其进行测试,因此如果遇到异常,您可能需要对其进行修改.还用硬编码显示了解决问题的方法.

This is how I would approach it. I have not tested it so you may have modify it if you encounter exception. It is all also hard coded to show the approach to solved the problem.

这篇关于更改密码后,从所有浏览器注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆