You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Register.cshtml.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.ComponentModel.DataAnnotations;
  2. using System.Text;
  3. using System.Text.Encodings.Web;
  4. using Microsoft.AspNetCore.Authentication;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Identity;
  7. using Microsoft.AspNetCore.Identity.UI.Services;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Mvc.RazorPages;
  10. using Microsoft.AspNetCore.WebUtilities;
  11. namespace SecureSharing.Areas.Identity.Pages.Account;
  12. [AllowAnonymous]
  13. public sealed class RegisterModel : PageModel
  14. {
  15. private readonly IEmailSender _emailSender;
  16. private readonly ILogger<RegisterModel> _logger;
  17. private readonly SignInManager<IdentityUser> _signInManager;
  18. private readonly UserManager<IdentityUser> _userManager;
  19. public RegisterModel(
  20. UserManager<IdentityUser> userManager,
  21. SignInManager<IdentityUser> signInManager,
  22. ILogger<RegisterModel> logger,
  23. IEmailSender emailSender)
  24. {
  25. _userManager = userManager;
  26. _signInManager = signInManager;
  27. _logger = logger;
  28. _emailSender = emailSender;
  29. }
  30. [BindProperty] public InputModel Input { get; set; }
  31. public string ReturnUrl { get; set; }
  32. public IList<AuthenticationScheme> ExternalLogins { get; set; }
  33. public async Task OnGetAsync(string returnUrl = null)
  34. {
  35. ReturnUrl = returnUrl;
  36. ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
  37. }
  38. public async Task<IActionResult> OnPostAsync(string returnUrl = null)
  39. {
  40. returnUrl ??= Url.Content("~/");
  41. ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
  42. if (ModelState.IsValid)
  43. {
  44. var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
  45. var result = await _userManager.CreateAsync(user, Input.Password);
  46. if (result.Succeeded)
  47. {
  48. _logger.LogInformation("User created a new account with password.");
  49. var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
  50. code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
  51. var callbackUrl = Url.Page(
  52. "/Account/ConfirmEmail",
  53. null,
  54. new { area = "Identity", userId = user.Id, code, returnUrl },
  55. Request.Scheme);
  56. await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
  57. $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
  58. if (_userManager.Options.SignIn.RequireConfirmedAccount)
  59. return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl });
  60. await _signInManager.SignInAsync(user, false);
  61. return LocalRedirect(returnUrl);
  62. }
  63. foreach (var error in result.Errors) ModelState.AddModelError(string.Empty, error.Description);
  64. }
  65. // If we got this far, something failed, redisplay form
  66. return Page();
  67. }
  68. public sealed class InputModel
  69. {
  70. [Required]
  71. [EmailAddress]
  72. [Display(Name = "Email")]
  73. public string Email { get; set; }
  74. [Required]
  75. [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.",
  76. MinimumLength = 6)]
  77. [DataType(DataType.Password)]
  78. [Display(Name = "Password")]
  79. public string Password { get; set; }
  80. [DataType(DataType.Password)]
  81. [Display(Name = "Confirm password")]
  82. [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
  83. public string ConfirmPassword { get; set; }
  84. }
  85. }