September 22, 2019

ASP.NET Identity reset password

While learning ASP.NET Identity system, when used in my first ASP.Net Web Forms project I faced the problem how to reset a user's password. I found the following code works for me.

ApplicationDbContext context = new ApplicationDbContext();
UserStore userStore = new UserStore(context);
UserManager userManager = new UserManager(userStore);
String userId = "myuser"; //user name here...
String newPassword = "mypassword"; // password here...
String hashedNewPassword = userManager.PasswordHasher.HashPassword(newPassword);
ApplicationUser curentUser = await userStore.FindByIdAsync(userId);
await userStore.SetPasswordHashAsync(curentUser, hashedNewPassword);
await userStore.UpdateAsync(curentUser);

No comments:

Post a Comment