| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | #nullable disable |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.ComponentModel.DataAnnotations; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Text.Encodings.Web; |
| | | 11 | | using System.Threading; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | using Microsoft.AspNetCore.Authentication; |
| | | 14 | | using Microsoft.AspNetCore.Authorization; |
| | | 15 | | using Microsoft.AspNetCore.Identity; |
| | | 16 | | using Microsoft.AspNetCore.Identity.UI.Services; |
| | | 17 | | using Microsoft.AspNetCore.Mvc; |
| | | 18 | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | 19 | | using Microsoft.AspNetCore.WebUtilities; |
| | | 20 | | using Microsoft.Extensions.Logging; |
| | | 21 | | using OzonRepositories.Context.Identity; |
| | | 22 | | |
| | | 23 | | namespace OzonOrdersWeb.Areas.Identity.Pages.Account |
| | | 24 | | { |
| | | 25 | | [Authorize(Roles = "Admin")] |
| | | 26 | | public class RegisterModel : PageModel |
| | | 27 | | { |
| | | 28 | | private readonly SignInManager<CustomIdentityUser> _signInManager; |
| | | 29 | | private readonly UserManager<CustomIdentityUser> _userManager; |
| | | 30 | | private readonly IUserStore<CustomIdentityUser> _userStore; |
| | | 31 | | private readonly IUserEmailStore<CustomIdentityUser> _emailStore; |
| | | 32 | | private readonly ILogger<RegisterModel> _logger; |
| | | 33 | | |
| | 0 | 34 | | public RegisterModel( |
| | 0 | 35 | | UserManager<CustomIdentityUser> userManager, |
| | 0 | 36 | | IUserStore<CustomIdentityUser> userStore, |
| | 0 | 37 | | SignInManager<CustomIdentityUser> signInManager, |
| | 0 | 38 | | ILogger<RegisterModel> logger) |
| | | 39 | | { |
| | 0 | 40 | | _userManager = userManager; |
| | 0 | 41 | | _userStore = userStore; |
| | 0 | 42 | | _emailStore = GetEmailStore(); |
| | 0 | 43 | | _signInManager = signInManager; |
| | 0 | 44 | | _logger = logger; |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 49 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 50 | | /// </summary> |
| | | 51 | | [BindProperty] |
| | 0 | 52 | | public InputModel Input { get; set; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 56 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 57 | | /// </summary> |
| | 0 | 58 | | public string ReturnUrl { get; set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 62 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 63 | | /// </summary> |
| | 0 | 64 | | public IList<AuthenticationScheme> ExternalLogins { get; set; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 68 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 69 | | /// </summary> |
| | | 70 | | public class InputModel |
| | | 71 | | { |
| | | 72 | | /// <summary> |
| | | 73 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 74 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 75 | | /// </summary> |
| | | 76 | | [Required] |
| | | 77 | | [EmailAddress] |
| | | 78 | | [Display(Name = "Email")] |
| | 0 | 79 | | public string Email { get; set; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 83 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 84 | | /// </summary> |
| | | 85 | | [Required] |
| | | 86 | | [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLen |
| | | 87 | | [DataType(DataType.Password)] |
| | | 88 | | [Display(Name = "Password")] |
| | 0 | 89 | | public string Password { get; set; } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 93 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 94 | | /// </summary> |
| | | 95 | | [DataType(DataType.Password)] |
| | | 96 | | [Display(Name = "Confirm password")] |
| | | 97 | | [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] |
| | 0 | 98 | | public string ConfirmPassword { get; set; } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | |
| | | 102 | | public async Task OnGetAsync(string returnUrl = null) |
| | | 103 | | { |
| | 0 | 104 | | ReturnUrl = returnUrl; |
| | 0 | 105 | | ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public async Task<IActionResult> OnPostAsync(string returnUrl = null) |
| | | 109 | | { |
| | 0 | 110 | | returnUrl ??= Url.Content("~/"); |
| | 0 | 111 | | ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); |
| | 0 | 112 | | if (ModelState.IsValid) |
| | | 113 | | { |
| | 0 | 114 | | var user = CreateUser(); |
| | | 115 | | |
| | 0 | 116 | | await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None); |
| | 0 | 117 | | await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None); |
| | 0 | 118 | | var result = await _userManager.CreateAsync(user, Input.Password); |
| | | 119 | | |
| | 0 | 120 | | if (result.Succeeded) |
| | | 121 | | { |
| | 0 | 122 | | _logger.LogInformation("User created a new account with password."); |
| | | 123 | | |
| | 0 | 124 | | var userId = await _userManager.GetUserIdAsync(user); |
| | 0 | 125 | | var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); |
| | 0 | 126 | | code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); |
| | 0 | 127 | | var callbackUrl = Url.Page( |
| | 0 | 128 | | "/Account/ConfirmEmail", |
| | 0 | 129 | | pageHandler: null, |
| | 0 | 130 | | values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl }, |
| | 0 | 131 | | protocol: Request.Scheme); |
| | | 132 | | |
| | | 133 | | |
| | 0 | 134 | | if (_userManager.Options.SignIn.RequireConfirmedAccount) |
| | | 135 | | { |
| | 0 | 136 | | return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl } |
| | | 137 | | } |
| | | 138 | | else |
| | | 139 | | { |
| | 0 | 140 | | await _signInManager.SignInAsync(user, isPersistent: false); |
| | 0 | 141 | | return LocalRedirect(returnUrl); |
| | | 142 | | } |
| | | 143 | | } |
| | 0 | 144 | | foreach (var error in result.Errors) |
| | | 145 | | { |
| | 0 | 146 | | ModelState.AddModelError(string.Empty, error.Description); |
| | | 147 | | } |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | // If we got this far, something failed, redisplay form |
| | 0 | 151 | | return Page(); |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | private CustomIdentityUser CreateUser() |
| | | 155 | | { |
| | | 156 | | try |
| | | 157 | | { |
| | 0 | 158 | | return Activator.CreateInstance<CustomIdentityUser>(); |
| | | 159 | | } |
| | 0 | 160 | | catch |
| | | 161 | | { |
| | 0 | 162 | | throw new InvalidOperationException($"Can't create an instance of '{nameof(CustomIdentityUser)}'. " + |
| | 0 | 163 | | $"Ensure that '{nameof(CustomIdentityUser)}' is not an abstract class and has a parameterless constr |
| | 0 | 164 | | $"override the register page in /Areas/Identity/Pages/Account/Register.cshtml"); |
| | | 165 | | } |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | private IUserEmailStore<CustomIdentityUser> GetEmailStore() |
| | | 169 | | { |
| | 0 | 170 | | if (!_userManager.SupportsUserEmail) |
| | | 171 | | { |
| | 0 | 172 | | throw new NotSupportedException("The default UI requires a user store with email support."); |
| | | 173 | | } |
| | 0 | 174 | | return (IUserEmailStore<CustomIdentityUser>)_userStore; |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | } |