ChangePasswordasync返回错误密码错误



我正在尝试发送 POST请求并使用它更改用户密码,但是我得到

http 409和不正确的密码错误消息

if (request.Password != null)
{
    var hashedPassword = new PasswordHasher<User>().HashPassword(user, request.Password);
    var changePasswordResult = await _userManager.ChangePasswordAsync(user, user.PasswordHash, hashedPassword);
    if (!changePasswordResult.Succeeded)
    {
        return StatusCode((int)HttpStatusCode.Conflict, new ErrorResponse
        {
            ErrorMessage = string.Join(Environment.NewLine, changePasswordResult.Errors.Select(x => x.Description))
        });
    }
}

或我应该这样更改密码?

var hashedPassword = new PasswordHasher<User>().HashPassword(user, request.Password);   
user.PasswordHash = hashedPassword;

首先,我从请求中验证密码,如果有效,我将其更新

if (request.Password != null)
            {
                var valid = await new PasswordValidator<User>().ValidateAsync(_userManager, user, request.Password);
                if (!valid.Succeeded)
                {
                    return StatusCode((int)HttpStatusCode.Conflict, new ErrorResponse
                    {
                        ErrorMessage = string.Join(Environment.NewLine, valid.Errors.Select(x => x.Description))
                    });
                }
                var hashedPassword = new PasswordHasher<User>().HashPassword(user, request.Password);
                user.PasswordHash = hashedPassword;
            }

相关内容

  • 没有找到相关文章

最新更新