Updating a users password using ASP.NET Identity 1.0

I've inherited a WebSite which, is using ASP.NET Identity version 1.0 for user managment, and have a situation, where we quickly need to roll out some functionality to allow a user to reset their password. We fully, intend to migrate this site onto the latest and greatest, the MS have to offer, but for the time being, we just need this to work.

I'v therefore, decompiled the Identity dll, and have tried calling it as follows:

  protected async void ForgotPassword(object sender, EventArgs e)
            string password = Membership.GeneratePassword(12, 1);
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

            ApplicationUser user = manager.FindById(User.Identity.GetUserId());
            await manager.AddPasswordAsync(User.Identity.GetUserId(), password);

            //Send out email.
            GlobalMethods.SendPasswordChangeNotification(user.UserName, user.Email, password);

Open in new window

I get me email, with the random password, but the physicall PasswordHash and SecurityStamp in the underlining table (AspNetUser), isn't  updated at all.

Can you please assist?
Wayne AthertonProduct Analyst Financial Messaging LondonAsked:
Who is Participating?
Randy DownsOWNERCommented:
Maybe this will help.

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

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.