我尝试发送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;
答案 0 :(得分:0)
首先,我根据请求验证密码,如果它有效,我会更新
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;
}