public async Task <IActionResult> OnGetAsync(string userId, string email, string code) { // Check if the user ID, e-mail and code aren't provided. if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(code)) { // Display an error. TempData["StatusMessage"] = "Error: The confirmation link is not valid. Please try to paste the link manually into the browser address bar."; // Redirect to the home page. return(RedirectToPage("/Index")); } // Get the user with the provided user ID. var user = await _userManager.FindByIdAsync(userId); // Check if there wasn't any user found. if (user == null) { // Display an error. TempData["StatusMessage"] = "Error: The user ID in the confirmation link is not valid. Please try to paste the link manually into the browser address bar."; // Redirect to the home page. return(RedirectToPage("/Index")); } // Check if an account with the new e-mail address already exists. if (await _userManager.FindByEmailAsync(email) != null) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An account with the new e-mail address already exists."); // Return the page. return(Page()); } // Get the current e-mail of the user. var oldEmail = user.Email; // Try to change the e-mail using the provided code. var result = await _userManager.ChangeEmailAsync(user, email, code); // Check if the confirmation failed. if (!result.Succeeded) { // Display an error. TempData["StatusMessage"] = "Error: The e-mail or the confirmation code in the link are not valid. Please try to paste the link manually into the browser address bar."; // Redirect to the home page. return(RedirectToPage("/Index")); } // Define a new task. var usersTask = new UsersTask { Items = new List <UserInputModel> { new UserInputModel { Id = user.Id, Email = user.Email, EmailConfirmed = true } } }; // Try to run the task. try { // Run the task. await usersTask.EditAsync(_serviceProvider, CancellationToken.None); } catch (Exception) { // Display an error. TempData["StatusMessage"] = "Error: There was an error with setting the new e-mail address."; // Redirect to the home page. return(RedirectToPage("/Index")); } // Define a new view model for the e-mail. var emailChangedEmailViewModel = new EmailEmailChangedViewModel { OldEmail = oldEmail, NewEmail = user.Email, Url = _linkGenerator.GetUriByPage(HttpContext, "/Account/Index", handler: null, values: null), ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null) }; // Send the e-mail changed e-mail to the user. await _emailSender.SendEmailChangedEmailAsync(emailChangedEmailViewModel); // Re-sign in the user to update the changes. await _signInManager.RefreshSignInAsync(user); // Display a message to the user. TempData["StatusMessage"] = "Success: Thank you for confirming your e-mail. You can now log in and use the application."; // Redirect to the login page. return(RedirectToPage("/Identity/Login")); }
public async Task <IActionResult> OnPost() { // Check if there isn't any ID provided. if (string.IsNullOrEmpty(Input.Id)) { // Display a message. TempData["StatusMessage"] = "Error: No ID has been provided."; // Redirect to the index page. return(RedirectToPage("/Administration/Accounts/Users/Index")); } // Define the query. var query = _context.Users .Where(item => item.Id == Input.Id); // Define the view. View = new ViewModel { User = query .FirstOrDefault() }; // Check if the item hasn't been found. if (View.User == null) { // Display a message. TempData["StatusMessage"] = "Error: No item could be found with the provided ID."; // Redirect to the index page. return(RedirectToPage("/Administration/Accounts/Users/Index")); } // Check if the provided model isn't valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields."); // Redisplay the page. return(Page()); } // Define a new task. var task = new UsersTask { Items = new List <UserInputModel> { new UserInputModel { Id = Input.Id, Email = Input.Email, EmailConfirmed = Input.EmailConfirmed } } }; // Try to run the task. try { // Run the task. await task.EditAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } // Display a message. TempData["StatusMessage"] = $"Success: 1 user updated successfully."; // Redirect to the index page. return(RedirectToPage("/Administration/Accounts/Users/Index")); }