renakre
renakre

Reputation: 8291

ResetPasswordAsync returns success but does not change the password

I have the following code to reset the password of a user (who actually does not have a password):

ApplicationUser u1 =  _userManager.FindByEmailAsync("[email protected]").Result;
string resetToken1 = _userManager.GeneratePasswordResetTokenAsync(u1).Result;
IdentityResult r1 = _userManager.ResetPasswordAsync(u1, resetToken1, "12345Da*").Result;

r1 returns succeed but the password is indeed not changed. I cannot login with the new password. Any suggestions? What I am missing?

Upvotes: 1

Views: 1036

Answers (1)

Tony
Tony

Reputation: 20082

You should use async and await in this case to execute your code

Below is my example

[HttpPost("ResetPassword"), ValidModel, AllowAnonymous]
public async Task<IActionResult> ResetPassword([FromBody]ResetPasswordViewModel model)
{
    if (string.IsNullOrEmpty(model.Token) || string.IsNullOrEmpty(model.Email))
    {
        return RedirectToAction("Index", "Error", new { statusCode = AppStatusCode.NotFound });
    }

    var isResetTokenValid = await _userManager.CheckValidResetPasswordToken(model.Token, model.Email);

    if (!isResetTokenValid || string.IsNullOrEmpty(model.Email))
    {
        return StatusCode(AppStatusCode.ResetPassTokenExpire);
    }

    var user = await _userManager.FindByEmailAsync(model.Email);
    if (user == null)
    {
        return Ok();
    }

    await _userManager.ResetPasswordAsync(user, model.Token, model.Password); // use await here
    return Ok();
}

Upvotes: 1

Related Questions