简体   繁体   中英

IdentityUser how to verify old password?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM