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 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.