IdentityUser如何验证旧密码?

时间:2018-04-24 23:31:03

标签: c# asp.net-core asp.net-core-identity

我希望能够在他/她输入正确的旧密码时更改当前用户密码,如下所示:

Way of updating user password

但我一直在努力寻找优雅的解决方案,我的当前解决方案如下:

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);
    }
}

1 个答案:

答案 0 :(得分:3)

ChangePasswordAsync()方法需要对旧密码 进行哈希处理。

试试这个。

_userManager.ChangePasswordAsync(appUser, model.OldPassword, model.NewPassword);