public async Task <IActionResult> ModifyAccount() { var userName = User?.Identity?.Name; if (!string.IsNullOrEmpty(userName)) { var user = await _userManager.FindByNameAsync(userName); if (user != null) { var model = new UserSelfModifViewModel() { UserIdentity = user.Id, UserName = user.UserName, PhoneNumber = user.PhoneNumber, }; return(View(model)); } } return(RedirectToAction("Index", "Home")); }
public async Task <IActionResult> ModifyAccount(UserSelfModifViewModel model) { if (ModelState.IsValid) { var user = await _userManager.FindByIdAsync(model.UserIdentity); if (user != null) { var passwordOK = true; if (!string.IsNullOrEmpty(model.Password)) { passwordOK = await _userManager.CheckPasswordAsync(user, model.Password); } if (passwordOK) { if ((model.NewPassword == model.NewPassword2) || (string.IsNullOrEmpty(model.NewPassword) && string.IsNullOrEmpty(model.NewPassword2))) { IdentityResult validPassword = null; if (!string.IsNullOrEmpty(model.NewPassword)) { validPassword = await _passwordValidator.ValidateAsync(_userManager, user, model.NewPassword); if (validPassword.Succeeded) { user.PasswordHash = _passwordHasher.HashPassword(user, model.NewPassword); } else { this.AddErrorsFromResult(validPassword); } } if (validPassword == null || validPassword.Succeeded) { if (!string.IsNullOrEmpty(model.UserName)) { user.UserName = model.UserName; } if (!string.IsNullOrEmpty(model.PhoneNumber)) { user.PhoneNumber = Convert.ToInt32(model.PhoneNumber).ToString(); } IdentityResult result = await _userManager.UpdateAsync(user); if (result.Succeeded) { return(RedirectToAction("Logout", "Login", new { id = user.Id })); } else { this.AddErrorsFromResult(result); } } } else { ModelState.AddModelError("", ApplicationResources.UserInterface.Common.NewPasswordsNotEqual); } } else { ModelState.AddModelError("", ApplicationResources.UserInterface.Common.WrongCurrentPassword); } } else { ModelState.AddModelError("", ApplicationResources.UserInterface.Common.UserDoNotExist); } } return(View(model)); }