How to reset password with UserManager of ASP.NET MVC 5 [duplicate]
Asked Answered
B

7

49

I am wondering if there is a way to reset password with UserManager of ASP.NET MVC 5

I tried this with user that already has a password but no success. Any clue?

IdentityResult result = UserManager.AddPassword(forgotPasswordEvent.UserId.ToString(), model.ConfirmPassword);
if (result.Succeeded)
{
       //
}
else
{
        AddErrors(result);
}
Bolide answered 19/3, 2014 at 19:59 Comment(0)
B
94

It is here ASP.NET Identity reset password

UserManager<IdentityUser> userManager = 
    new UserManager<IdentityUser>(new UserStore<IdentityUser>());

userManager.RemovePassword(userId);

userManager.AddPassword(userId, newPassword);
Bolide answered 20/3, 2014 at 13:12 Comment(4)
And what do you do if addpassword fails? now user has no password? there are better options here: #19524611Muth
@Muth maybe transactionScope is possible solution :msdn.microsoft.com/en-us/library/…Dominican
If you have the code in the standard AccountController.cs, you can just use the UserManager available there.Cathey
This site has a working example which includes handling results of the calls codereview.stackexchange.com/questions/92737/…Hair
D
31

I suppose this is newer but there is such an API in Identity 2.0:

IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);

model.Code is generated the following way, and you should send this as a link in a email to make sure the user who is claiming to want to change the password is that one that owns the email address:

string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
Dipsomaniac answered 13/9, 2014 at 22:3 Comment(2)
UserManager.ResetPasswordAsync(user.Id, UserManager.GeneratePasswordResetTokenAsync(user.Id), model.Password);Pilatus
is the reset code saved in database ? How it check when we call resetpasswordasync method ?Poulos
B
8
var validPass= await userManager.PasswordValidator.ValidateAsync(txtPassword1.Text);
if(validPass.Succeeded)
{
    var user = userManager.FindByName(currentUser.LoginName);
    user.PasswordHash = userManager.PasswordHasher.HashPassword(txtPassword1.Text);
    var res= userManager.Update(user);
    if(res.Succeeded)
    {
        // change password has been succeeded
    }
}
Bodywork answered 29/6, 2018 at 14:30 Comment(0)
U
6

try using the user store:

 var user = UserManager.FindById(forgotPasswordEvent.UserId);

 UserStore<ApplicationUser> store = new UserStore<ApplicationUser>();
 store.SetPasswordHashAsync(user, uManager.PasswordHasher.HashPassword(model.ConfirmPassword));

The IdentityMembership is cool, but still lacking some implementation

UPDATE

Identity 2.0 is here now and has a lot more features

Unmeasured answered 19/3, 2014 at 20:9 Comment(1)
I tried your sample but then I get a TimeOutException. I'm still using Identity 1.0 and I don't think I can migrate as it would require major refactoring in my application. Am using Microsoft.Owin 2.10 and OWIN 1.0Overt
B
4

Try this code .It is working perfectly:

    var userStore = new UserStore<IdentityUser>();

    var userManager = new UserManager<IdentityUser>(userStore);

    string userName= UserName.Text;

    var user =userManager.FindByName(userName);
    if (user.PasswordHash != null  )
    {
        userManager.RemovePassword(user.Id);
    }

    userManager.AddPassword(user.Id, newpassword);
Birdella answered 4/3, 2017 at 5:43 Comment(4)
Welcome to Stack Overflow! In order to make sure your answer is as helpful as possible to as broad an audience as possible, you might consider revising it to include (1) an evaluation of what was flawed about the existing code, (2) how your code avoids such pitfalls, and (3) any assumptions or shortcomings in your solution. Have a look here for inspiration. Thanks again for posting an answer, and I hope to see more from you in the future.Aggrandize
how is this different from the current accepted answer?Anthology
the old answer does not tell how to get the userd and pass it as user.id like this: string userName= UserName.Text; var user =userManager.FindByName(userName); it didn't check the condition if the password has null value .Birdella
@GaneshPMP: That's a valid reply. So incorporate that into your answer.Veradia
P
3

I added this to my UserManager class :

    public virtual async Task<IdentityResult> UpdatePassword(ApplicationUser user, string newPassword)
    {
        var passwordStore = Store as IUserPasswordStore<ApplicationUser, string>;

        if (passwordStore == null)
            throw new Exception("UserManager store does not implement IUserPasswordStore");

        var result = await base.UpdatePassword(passwordStore, user, newPassword);

        if (result.Succeeded)
            result = await base.UpdateAsync(user);

        return result;
    }
Paleobiology answered 25/10, 2015 at 17:42 Comment(4)
what is you base class here?Overt
Sorry about the delay, its UserManager<ApplicationUser>. ApplicationUser is derived from IdentityUser. The UserManager has a property "Store" that is (usually) an instance of IUserPasswordStore<YourUserClass,YourIdType>.Paleobiology
Thanks, I had already figured that out, but I don't see any UpdatePassword method, could you please tell me what version of Identity and OWIN are you using?Overt
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net46" />Paleobiology
F
2

There are extension to change the password in the namespace Microsoft.AspNet.Identity.

https://msdn.microsoft.com/en-us/library/dn497466(v=vs.108).aspx

Fulgurant answered 30/5, 2017 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.