locked
Reset a password in Asp.net Identity RRS feed

  • Question

  • User269881539 posted

    We have a site using Asp.Net Identity to establish user accounts.

    It uses the standard password reset functionality of generating a token etc and emailing the users the reset links.

    Sometimes though the users do not receive the emails - so is there a way that we can reset their passwords to a value that we generate so we can at least let the users back in with a temporary password, which they can then alter once they gain access back to their account?

    All the in built code relies on them being authenticated and identifiable obviously - we would need to do this via some sort of back end management I guess?

    Thanks

    Wednesday, May 29, 2019 7:21 PM

All replies

  • User475983607 posted

    You need the PasswordHash  An easy way to get the PasswordHash is to create a new account then grab the PasswordHash from the user table.  From there a reset is just a matter of updating the user's record with the PasswordHash.

    Wednesday, May 29, 2019 7:56 PM
  • User-1038772411 posted

    Hi, Chilluk

    In current release

    Assuming you have handled the verification of the request to reset the forgotten password, use following code as a sample code steps.

    ApplicationDbContext =new ApplicationDbContext()
    String userId = "<YourLogicAssignsRequestedUserId>";
    String newPassword = "<PasswordAsTypedByUser>";
    ApplicationUser cUser = UserManager.FindById(userId);
    String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);
    UserStore<ApplicationUser> store = new UserStore<ApplicationUser>();            
    store.SetPasswordHashAsync(cUser, hashedNewPassword);

    In AspNet Nightly Build

    The framework is updated to work with Token for handling requests like ForgetPassword. Once in release, simple code guidance is expected.

    Update:

    This update is just to provide more clear steps.

    ApplicationDbContext context = new ApplicationDbContext();
    UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
    UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store);
    String userId = User.Identity.GetUserId();//"<YourLogicAssignsRequestedUserId>";
    String newPassword = "test@123"; //"<PasswordAsTypedByUser>";
    String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);                    
    ApplicationUser cUser = await store.FindByIdAsync(userId);
    await store.SetPasswordHashAsync(cUser, hashedNewPassword);
    await store.UpdateAsync(cUser);

    Thanks.

    Monday, June 10, 2019 1:15 PM