2017-12-18 269 views
1

我正在開發一個API,其中我實現了Login和ResetPassword功能。登錄工作正常,並且resetPassword也可以正常工作。但是,當我重置密碼並嘗試使用新密碼登錄時,登錄失敗。重置後,我可以看到哈希和密碼字段得到更新,但登錄失敗。我正在使用下面的代碼進行重置。身份MVC更改密碼,然後登錄

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

如果你想使用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(); 
} 

您可以使用ASLO和RemovePasswordAsync一起AddPasswordAsync更改密碼:

await UserManager.RemovePasswordAsync(userId.Id); 

var result = UserManager.AddPasswordAsync(NewPassword); 
相關問題