public async Task <IActionResult> ChangePassword(AccountDashboard userInfo) { if (!ModelState.IsValid) { ModelState.AddModelError(string.Empty, string.Empty); return(View(userInfo)); } if (!User.Identity.IsAuthenticated) { return(RedirectToAction(nameof(AccountController.Login))); } var user = await _userManager.FindByNameAsync(User.Identity.Name); if (user != null) { var isExisted = await _signInManager.PasswordSignInAsync(user, userInfo.OldPassword, isPersistent : false, lockoutOnFailure : false); if (isExisted.Succeeded) { user.Address = userInfo.Address; user.PhoneNumber = userInfo.PhoneNumber; user.BirthDay = userInfo.BirthDay; user.FullName = userInfo.FullName; var IsUpdated = await _userManager.UpdateAsync(user); if (!IsUpdated.Succeeded) { ModelState.AddModelError(string.Empty, string.Empty); return(View(userInfo)); } if (string.IsNullOrEmpty(userInfo.NewPassword)) { return(RedirectToAction(nameof(AccountController.ChangePassword))); } var token = await _userManager.GeneratePasswordResetTokenAsync(user); var result = await _userManager.ResetPasswordAsync(user, token, userInfo.NewPassword); if (result.Succeeded) { return(RedirectToAction(nameof(AccountController.ChangePassword))); } else { ModelState.AddModelError(string.Empty, string.Empty); return(View(userInfo)); } } } return(RedirectToAction(nameof(AccountController.Login))); }
public async Task <IActionResult> ChangePassword(Guid userId) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction(nameof(AccountController.Login))); } var user = await _userManager.FindByNameAsync(User.Identity.Name); var userDashboard = new AccountDashboard() { Address = user.Address, BirthDay = user.BirthDay, FullName = user.FullName, PhoneNumber = user.PhoneNumber, Email = user.Email }; return(View(userDashboard)); }