How to reset password with UserManager of ASP.NET MVC 5

后端 未结 7 1535
情深已故
情深已故 2021-01-30 06:36

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 bu

相关标签:
7条回答
  • 2021-01-30 07:10

    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

    0 讨论(0)
  • 2021-01-30 07:24

    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;
        }
    
    0 讨论(0)
  • 2021-01-30 07:25

    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);
    
    0 讨论(0)
  • 2021-01-30 07:29

    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);
    
    0 讨论(0)
  • 2021-01-30 07:32
    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
        }
    }
    
    0 讨论(0)
  • 2021-01-30 07:34

    It is here ASP.NET Identity reset password

    UserManager<IdentityUser> userManager = 
        new UserManager<IdentityUser>(new UserStore<IdentityUser>());
    
    userManager.RemovePassword(userId);
    
    userManager.AddPassword(userId, newPassword);
    
    0 讨论(0)
提交回复
热议问题