I would like to be able to change the current user password only if he/she inputs the right old password, like this:
But I've been strugling to find here and elsewhere an elegant solution,
my current solution looks like this:
var oldPasswordHashed = _userManager.PasswordHasher.HashPassword(appUser, model.OldPassword);
if (oldPasswordHashed == appUser.PasswordHash)
{
var result = await _userManager.ChangePasswordAsync(appUser, appUser.PasswordHash, model.NewPassword);
if (!result.Succeeded)
{
ModelState.AddModelError(nameof(EditUserViewModel.OldPassword), "Error at changing password, retry later.");
return View(model);
}
}
The ChangePasswordAsync()
method expects the old password not to be hashed.
Try this instead.
_userManager.ChangePasswordAsync(appUser, model.OldPassword, model.NewPassword);