public async Task <IActionResult> OnPost() { // 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 { Email = Input.Email, Type = "Password", Data = JsonSerializer.Serialize(Input.Password), EmailConfirmed = Input.EmailConfirmed } } }; // Try to run the task. try { // Run the task. await task.CreateAsync(_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 created successfully."; // Redirect to the index page. return(RedirectToPage("/Administration/Accounts/Users/Index")); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { // Define the variables for the view. View = new ViewModel { ReturnUrl = returnUrl ?? _linkGenerator.GetPathByPage(HttpContext, "/Index", handler: null, values: null) }; // Check if the reCaptcha is valid. if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken)) { // Add an error to the model. ModelState.AddModelError(string.Empty, "The reCaptcha verification failed."); // Return the page. return(Page()); } // Check if the provided model is not valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error was encountered. Please check again the input fields."); // Return the page. return(Page()); } // Define a new task. var task = new UsersTask { Items = new List <UserInputModel> { new UserInputModel { Email = Input.Email, Type = "Password", Data = JsonSerializer.Serialize(Input.Password) } } }; // Try to run the task. try { // Run the task. await task.CreateAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } // Get the new user. var user = await _userManager.FindByEmailAsync(Input.Email); // Check if there wasn't any user found. if (user == null) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error was encountered. Please try again."); // Redisplay the page. return(Page()); } // Generate an e-mail confirmation code. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); // Create the callback URL to be encoded in the confirmation email. var callbackUrl = _linkGenerator.GetUriByPage(HttpContext, "/Identity/ConfirmEmail", handler: null, values: new { userId = user.Id, code = code }); // Define a new view model for the e-mail. var emailViewModel = new EmailEmailConfirmationViewModel { Email = user.Email, Url = callbackUrl, ApplicationUrl = _linkGenerator.GetUriByPage(HttpContext, "/Index", handler: null, values: null) }; // Send the confirmation e-mail for the user. await _emailSender.SendEmailConfirmationEmailAsync(emailViewModel); // Display a message to the user. TempData["StatusMessage"] = "Success: The account has been created successfully. Please check the provided e-mail address for instructions on confirming your e-mail, in order to log in."; // Redirect to the return URL. return(LocalRedirect(View.ReturnUrl)); }