I have a passwordChange form to change the user password when is his first time login change it. After change the password I'm redirecting to the login page. The issue is that while Im in the changePasswordForm if I open a new tab, or close and open again the browser the user get logon, Im skipping the password change if is first time.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ResetPassword(ResetPasswordInputModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await _accountService.ResetPasswordAsync(model);
if (!result.Succeeded)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
return View();
}
TempData["IsPasswordChanged"] = true;
return RedirectToAction("Login", routeValues: new { ReturnUrl = model.ReturnUrl });
}
public async Task<IdentityResult> ResetPasswordAsync(ResetPasswordInputModel model)
{
var user = await _userManager.FindByNameAsync(model.Username);
var error = new IdentityError();
if (user == null)
{
error.Description = "User doesnt exist";
return IdentityResult.Failed(error);
}
var cantUpdatePassword = await _userManager.CheckPasswordAsync(user, model.NewPassword);
if (cantUpdatePassword)
{
error.Description = "Current password incorrect";
return IdentityResult.Failed(error);
}
var result = await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);
if (result.Succeeded)
{
user.IsPasswordChanged = true;
await _userManager.UpdateAsync(user);
// THIS IS NOT WORKING
await _signInManager.SignOutAsync();
await _events.RaiseAsync(new UserLogoutSuccessEvent(user.Id.ToString(), user.UserName));
}
return result;
}