public IActionResult ConfirmEmailChange([FromQuery] string code, [FromQuery] string oldEmail, [FromQuery] string newEmail) { if (code == null || oldEmail == null || newEmail == null) { return(RedirectToAction(nameof(HomeController.Index), "Home")); } var model = new ConfirmEmailChangeViewModel { Code = code, OldEmail = oldEmail, NewEmail = newEmail }; return(View(model)); }
public async Task <IActionResult> ConfirmEmailChange(ConfirmEmailChangeViewModel model) { if (model.Code == null || model.OldEmail == null || model.NewEmail == null) { return(RedirectToAction(nameof(HomeController.Index), "Home")); } var user = await _userManager.FindByEmailAsync(model.OldEmail); if (user == null) { this.FlashError("Invalid email code"); return(RedirectToAction(nameof(HomeController.Index), "Home")); } var existingUser = await _userManager.FindByEmailAsync(model.NewEmail); if (existingUser != null) { this.FlashError("Invalid email code"); return(RedirectToAction(nameof(HomeController.Index), "Home")); } // update user name user.UserName = model.NewEmail; // ChangeEmailAsync should persist this change if successful (it calls UpdateUserAsync) // change email var changeEmailResult = await _userManager.ChangeEmailAsync(user, model.NewEmail, model.Code); if (!changeEmailResult.Succeeded) { this.FlashError("Invalid email code"); return(RedirectToAction(nameof(HomeController.Index), "Home")); } this.FlashSuccess("Changed email"); return(RedirectToAction(nameof(HomeController.Index), "Home")); }