我正在开发一个API,我已经实现了Login和ResetPassword功能。登录工作正常,resetPassword也可以正常工作。但是当我重置密码并尝试使用新密码登录时,登录失败。重置后,我可以看到哈希和密码字段更新但登录失败。我正在使用以下代码进行重置。
if (user.VerificationCode == model.VerificationCode)
{
//var newPasswordHash = UserManager.PasswordHasher.HashPassword(model.NewPassword);
//var token = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
//user.PasswordHash = newPasswordHash;
user.Password = model.NewPassword;
//IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, token, newPasswordHash);
//IdentityResult result = await UserManager.ChangePasswordAsync(user.Id, user.Password, model.NewPassword);
var result = await UserManager.UpdateAsync(user);
if (!result.Succeeded)
{
response.Message = AppConstants.Error;
response.IsSuccess = false;
}
else
{
response.Message = AppConstants.OperationSuccessful;
response.IsSuccess = true;
}
}
使用SignInManager.PasswordSignInAsync登录已经给定的代码。 在这里做错了什么想法?
答案 0 :(得分:0)
如果要使用UpdateAsync
方法,则应首先哈希密码:
这种方法对我有用:
public async Task<IHttpActionResult> changePassword(UsercredentialsModel usermodel)
{
ApplicationUser user = await AppUserManager.FindByIdAsync(usermodel.Id);
if (user == null)
{
return NotFound();
}
user.PasswordHash = AppUserManager.PasswordHasher.HashPassword(usermodel.Password);
var result = await AppUserManager.UpdateAsync(user);
if (!result.Succeeded)
{
//throw exception......
}
return Ok();
}
您可以同时使用RemovePasswordAsync
和AddPasswordAsync
来更改密码:
await UserManager.RemovePasswordAsync(userId.Id);
var result = UserManager.AddPasswordAsync(NewPassword);