public async Task <IActionResult> ChangePassword(ChangePasswordViewModel changePasswordViewModel) { if (!ModelState.IsValid) { return(View(changePasswordViewModel)); } if (changePasswordViewModel.NewPassword != changePasswordViewModel.ConfirmNewPassword) { ModelState.AddModelError(string.Empty, "Le nouveau mot de passe ne correspond pas à la confirmation"); return(View(changePasswordViewModel)); } // Récupération de l'utilisateur Person User = await _userManager.GetUserAsync(HttpContext.User); var result = await _userManager.ChangePasswordAsync(User, changePasswordViewModel.Password, changePasswordViewModel.NewPassword); if (!result.Succeeded) { // Retour des erreurs foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } return(View(changePasswordViewModel)); } // Déconnexion await _signInManager.SignOutAsync(); return(Redirect("/")); }
public async Task <ActionResult> Manage(ManageUserViewModel model) { UserManager = new CUserManager(new CUserStore(UnitOfWork)); bool hasPassword = HasPassword(); ViewBag.HasLocalPassword = hasPassword; ViewBag.ReturnUrl = Url.Action("Manage"); if (hasPassword) { if (ModelState.IsValid) { IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId <int>(), model.OldPassword, model.NewPassword); if (result.Succeeded) { return(RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess })); } else { AddErrors(result); } } } else { // User does not have a password so remove any validation errors caused by a missing OldPassword field ModelState state = ModelState["OldPassword"]; if (state != null) { state.Errors.Clear(); } if (ModelState.IsValid) { IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId <int>(), model.NewPassword); if (result.Succeeded) { return(RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess })); } else { AddErrors(result); } } } // If we got this far, something failed, redisplay form return(View(model)); }