Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Identity reset password

How can I get the password of a user in the new ASP.NET Identity system? Or how can I reset without knowing the current one (user forgot password)?

like image 836
daniel Avatar asked Oct 22 '13 17:10

daniel


2 Answers

Or how can I reset without knowing the current one (user forgot password)?

If you want to change a password using the UserManager but you do not want to supply the user's current password, you can generate a password reset token and then use it immediately instead.

string resetToken = await UserManager.GeneratePasswordResetTokenAsync(model.Id); IdentityResult passwordChangeResult = await UserManager.ResetPasswordAsync(model.Id, resetToken, model.NewPassword); 
like image 60
Daniel Wright Avatar answered Oct 20 '22 07:10

Daniel Wright


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); 
like image 30
jd4u Avatar answered Oct 20 '22 07:10

jd4u