Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logout User From all Browser When Password is changed

I have a Reset Password page: enter image description here

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");
}

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.

like image 514
anand Avatar asked Feb 05 '16 09:02

anand


People also ask

Does changing password logout all devices?

Yes, when you changed your password you will not be automatically logout on other devices. You can do it manually if you need to logout all devices.

When you change your email password does it log out of other devices?

It is important you keep in mind that, after you change your password, you'll be logged out of every device associated with your account, except for the one used to change the password.

What if I change my email password?

What happens after you change your password. If you change or reset your password, you'll be signed out everywhere except: Devices you use to verify that it's you when you sign in. Some devices with third-party apps that you've given account access.


Video Answer


2 Answers

I saw you are using ASP.NET Identity 2. What you are trying to do is already built in. All you need to do is change the SecurityStamp and all previous authentication cookies are no longer valid.

After you change the password you also need to change the SecurityStamp:

await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
await UserManager.UpdateSecurityStampAsync(User.Identity.GetUserId());

If you want the user to remain logged in, you have to reissue a new authentication cookie (signin):

    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

Otherwise the user/session who initated the password change will also be logged out.

And to log out all other sessions immediately you need to lower the check interval in the 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))
    }
});

Steps to reproduce:

  1. Created a new Asp.Net Web App in VS2015.
  2. Choose MVC template.
  3. Edit App_Stat/Startup.Auth.cs, line 34: change validateInterval: TimeSpan.FromMinutes(30) to validateInterval: TimeSpan.FromSeconds(1)
  4. Edit Controllers/ManageController.cs, line 236: add the UserManager.UpdateSecurityStampAsync method call.
  5. Run project, create a user, login, open a different browser and also login.
  6. Change password, refresh the page in the other browser : you should be logged out.
like image 196
Chris Avatar answered Sep 19 '22 14:09

Chris


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.

Step 1

Add two properties to ApplicationUser model

enter image description here

Step 2

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.

enter image description here

Step 3

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

Step 4

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.

enter image description here

Step 5

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.

enter image description here

Lastly

Your AspnetUsers table should look as below

enter image description here

Comments

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.

like image 22
Julius Depulla Avatar answered Sep 18 '22 14:09

Julius Depulla