无法更改用户密码

Unable to change user's password

我有一个 MVC 5 Identity 2 应用程序。我正在尝试按如下方式更改用户密码:

    public async Task<string> ChangePassword()
    {
        var user = await this.UserManager.FindByIdAsync(18);
        PasswordHasher hasher = new PasswordHasher();

        user.PasswordHash = hasher.HashPassword("NewPassword");
        this.UserManager.Update(user);

        return string.Empty;
    }

this.UserManager 定义为:

HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

该方法执行成功,但密码未更改。我错过了一步吗?

我相信 UserManager 有这个方便的方法:

public virtual Task<IdentityResult> ChangePasswordAsync(
    TKey userId,
    string currentPassword,
    string newPassword
)

编辑:

这似乎对我有用。 UserManager 属性 定义如下:

public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

那么,密码重置方法:

//
    // POST: /Account/ResetPassword
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null)
        {
            // Don't reveal that the user does not exist
            return RedirectToAction("ResetPasswordConfirmation", "Account");
        }
        var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
        if (result.Succeeded)
        {
            return RedirectToAction("ResetPasswordConfirmation", "Account");
        }
        AddErrors(result);
        return View();
    }

UserManager 有这些方法来更改用户的密码:

public virtual Task<IdentityResult> ResetPasswordAsync(TKey userId, string token, string newPassword)

public virtual Task<IdentityResult> ChangePasswordAsync(TKey userId, string currentPassword, string newPassword);

protected virtual Task<IdentityResult> UpdatePassword(IUserPasswordStore<TUser, TKey> passwordStore, TUser user, string newPassword);