< Summary

Information
Class: OzonOrdersWeb.Areas.Identity.Pages.Account.RegisterModel
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Identity/Pages/Account/Register.cshtml.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 55
Coverable lines: 55
Total lines: 177
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_Input()100%210%
get_ReturnUrl()100%210%
get_ExternalLogins()100%210%
get_Email()100%210%
get_Password()100%210%
get_ConfirmPassword()100%210%
OnGetAsync()100%210%
OnPostAsync()0%110100%
CreateUser()100%210%
GetEmailStore()0%620%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Identity/Pages/Account/Register.cshtml.cs

#LineLine coverage
 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
 5using System;
 6using System.Collections.Generic;
 7using System.ComponentModel.DataAnnotations;
 8using System.Linq;
 9using System.Text;
 10using System.Text.Encodings.Web;
 11using System.Threading;
 12using System.Threading.Tasks;
 13using Microsoft.AspNetCore.Authentication;
 14using Microsoft.AspNetCore.Authorization;
 15using Microsoft.AspNetCore.Identity;
 16using Microsoft.AspNetCore.Identity.UI.Services;
 17using Microsoft.AspNetCore.Mvc;
 18using Microsoft.AspNetCore.Mvc.RazorPages;
 19using Microsoft.AspNetCore.WebUtilities;
 20using Microsoft.Extensions.Logging;
 21using OzonRepositories.Context.Identity;
 22
 23namespace 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
 034        public RegisterModel(
 035            UserManager<CustomIdentityUser> userManager,
 036            IUserStore<CustomIdentityUser> userStore,
 037            SignInManager<CustomIdentityUser> signInManager,
 038            ILogger<RegisterModel> logger)
 39        {
 040            _userManager = userManager;
 041            _userStore = userStore;
 042            _emailStore = GetEmailStore();
 043            _signInManager = signInManager;
 044            _logger = logger;
 045        }
 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]
 052        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>
 058        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>
 064        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")]
 079            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")]
 089            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.")]
 098            public string ConfirmPassword { get; set; }
 99        }
 100
 101
 102        public async Task OnGetAsync(string returnUrl = null)
 103        {
 0104            ReturnUrl = returnUrl;
 0105            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
 0106        }
 107
 108        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
 109        {
 0110            returnUrl ??= Url.Content("~/");
 0111            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
 0112            if (ModelState.IsValid)
 113            {
 0114                var user = CreateUser();
 115
 0116                await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
 0117                await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
 0118                var result = await _userManager.CreateAsync(user, Input.Password);
 119
 0120                if (result.Succeeded)
 121                {
 0122                    _logger.LogInformation("User created a new account with password.");
 123
 0124                    var userId = await _userManager.GetUserIdAsync(user);
 0125                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
 0126                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
 0127                    var callbackUrl = Url.Page(
 0128                        "/Account/ConfirmEmail",
 0129                        pageHandler: null,
 0130                        values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
 0131                        protocol: Request.Scheme);
 132
 133
 0134                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
 135                    {
 0136                        return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }
 137                    }
 138                    else
 139                    {
 0140                        await _signInManager.SignInAsync(user, isPersistent: false);
 0141                        return LocalRedirect(returnUrl);
 142                    }
 143                }
 0144                foreach (var error in result.Errors)
 145                {
 0146                    ModelState.AddModelError(string.Empty, error.Description);
 147                }
 0148            }
 149
 150            // If we got this far, something failed, redisplay form
 0151            return Page();
 0152        }
 153
 154        private CustomIdentityUser CreateUser()
 155        {
 156            try
 157            {
 0158                return Activator.CreateInstance<CustomIdentityUser>();
 159            }
 0160            catch
 161            {
 0162                throw new InvalidOperationException($"Can't create an instance of '{nameof(CustomIdentityUser)}'. " +
 0163                    $"Ensure that '{nameof(CustomIdentityUser)}' is not an abstract class and has a parameterless constr
 0164                    $"override the register page in /Areas/Identity/Pages/Account/Register.cshtml");
 165            }
 0166        }
 167
 168        private IUserEmailStore<CustomIdentityUser> GetEmailStore()
 169        {
 0170            if (!_userManager.SupportsUserEmail)
 171            {
 0172                throw new NotSupportedException("The default UI requires a user store with email support.");
 173            }
 0174            return (IUserEmailStore<CustomIdentityUser>)_userStore;
 175        }
 176    }
 177}