| | | 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.ComponentModel.DataAnnotations; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Linq; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Text.Encodings.Web; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using Microsoft.AspNetCore.Identity; |
| | | 13 | | using Microsoft.AspNetCore.Mvc; |
| | | 14 | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | 15 | | using Microsoft.Extensions.Logging; |
| | | 16 | | |
| | | 17 | | namespace OzonOrdersWeb.Areas.Identity.Pages.Account.Manage |
| | | 18 | | { |
| | | 19 | | public class EnableAuthenticatorModel : PageModel |
| | | 20 | | { |
| | | 21 | | private readonly UserManager<IdentityUser> _userManager; |
| | | 22 | | private readonly ILogger<EnableAuthenticatorModel> _logger; |
| | | 23 | | private readonly UrlEncoder _urlEncoder; |
| | | 24 | | |
| | | 25 | | private const string AuthenticatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&issuer={0}&digits=6"; |
| | | 26 | | |
| | 0 | 27 | | public EnableAuthenticatorModel( |
| | 0 | 28 | | UserManager<IdentityUser> userManager, |
| | 0 | 29 | | ILogger<EnableAuthenticatorModel> logger, |
| | 0 | 30 | | UrlEncoder urlEncoder) |
| | | 31 | | { |
| | 0 | 32 | | _userManager = userManager; |
| | 0 | 33 | | _logger = logger; |
| | 0 | 34 | | _urlEncoder = urlEncoder; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 39 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 40 | | /// </summary> |
| | 0 | 41 | | public string SharedKey { get; set; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 45 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public string AuthenticatorUri { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 51 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 52 | | /// </summary> |
| | | 53 | | [TempData] |
| | 0 | 54 | | public string[] RecoveryCodes { get; set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 58 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 59 | | /// </summary> |
| | | 60 | | [TempData] |
| | 0 | 61 | | public string StatusMessage { get; set; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 65 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 66 | | /// </summary> |
| | | 67 | | [BindProperty] |
| | 0 | 68 | | public InputModel Input { get; set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 72 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 73 | | /// </summary> |
| | | 74 | | public class InputModel |
| | | 75 | | { |
| | | 76 | | /// <summary> |
| | | 77 | | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used |
| | | 78 | | /// directly from your code. This API may change or be removed in future releases. |
| | | 79 | | /// </summary> |
| | | 80 | | [Required] |
| | | 81 | | [StringLength(7, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLengt |
| | | 82 | | [DataType(DataType.Text)] |
| | | 83 | | [Display(Name = "Verification Code")] |
| | 0 | 84 | | public string Code { get; set; } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public async Task<IActionResult> OnGetAsync() |
| | | 88 | | { |
| | 0 | 89 | | var user = await _userManager.GetUserAsync(User); |
| | 0 | 90 | | if (user == null) |
| | | 91 | | { |
| | 0 | 92 | | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | await LoadSharedKeyAndQrCodeUriAsync(user); |
| | | 96 | | |
| | 0 | 97 | | return Page(); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | public async Task<IActionResult> OnPostAsync() |
| | | 101 | | { |
| | 0 | 102 | | var user = await _userManager.GetUserAsync(User); |
| | 0 | 103 | | if (user == null) |
| | | 104 | | { |
| | 0 | 105 | | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | if (!ModelState.IsValid) |
| | | 109 | | { |
| | 0 | 110 | | await LoadSharedKeyAndQrCodeUriAsync(user); |
| | 0 | 111 | | return Page(); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Strip spaces and hyphens |
| | 0 | 115 | | var verificationCode = Input.Code.Replace(" ", string.Empty).Replace("-", string.Empty); |
| | | 116 | | |
| | 0 | 117 | | var is2faTokenValid = await _userManager.VerifyTwoFactorTokenAsync( |
| | 0 | 118 | | user, _userManager.Options.Tokens.AuthenticatorTokenProvider, verificationCode); |
| | | 119 | | |
| | 0 | 120 | | if (!is2faTokenValid) |
| | | 121 | | { |
| | 0 | 122 | | ModelState.AddModelError("Input.Code", "Verification code is invalid."); |
| | 0 | 123 | | await LoadSharedKeyAndQrCodeUriAsync(user); |
| | 0 | 124 | | return Page(); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | await _userManager.SetTwoFactorEnabledAsync(user, true); |
| | 0 | 128 | | var userId = await _userManager.GetUserIdAsync(user); |
| | 0 | 129 | | _logger.LogInformation("User with ID '{UserId}' has enabled 2FA with an authenticator app.", userId); |
| | | 130 | | |
| | 0 | 131 | | StatusMessage = "Your authenticator app has been verified."; |
| | | 132 | | |
| | 0 | 133 | | if (await _userManager.CountRecoveryCodesAsync(user) == 0) |
| | | 134 | | { |
| | 0 | 135 | | var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); |
| | 0 | 136 | | RecoveryCodes = recoveryCodes.ToArray(); |
| | 0 | 137 | | return RedirectToPage("./ShowRecoveryCodes"); |
| | | 138 | | } |
| | | 139 | | else |
| | | 140 | | { |
| | 0 | 141 | | return RedirectToPage("./TwoFactorAuthentication"); |
| | | 142 | | } |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | private async Task LoadSharedKeyAndQrCodeUriAsync(IdentityUser user) |
| | | 146 | | { |
| | | 147 | | // Load the authenticator key & QR code URI to display on the form |
| | 0 | 148 | | var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); |
| | 0 | 149 | | if (string.IsNullOrEmpty(unformattedKey)) |
| | | 150 | | { |
| | 0 | 151 | | await _userManager.ResetAuthenticatorKeyAsync(user); |
| | 0 | 152 | | unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user); |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | SharedKey = FormatKey(unformattedKey); |
| | | 156 | | |
| | 0 | 157 | | var email = await _userManager.GetEmailAsync(user); |
| | 0 | 158 | | AuthenticatorUri = GenerateQrCodeUri(email, unformattedKey); |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | private string FormatKey(string unformattedKey) |
| | | 162 | | { |
| | 0 | 163 | | var result = new StringBuilder(); |
| | 0 | 164 | | int currentPosition = 0; |
| | 0 | 165 | | while (currentPosition + 4 < unformattedKey.Length) |
| | | 166 | | { |
| | 0 | 167 | | result.Append(unformattedKey.AsSpan(currentPosition, 4)).Append(' '); |
| | 0 | 168 | | currentPosition += 4; |
| | | 169 | | } |
| | 0 | 170 | | if (currentPosition < unformattedKey.Length) |
| | | 171 | | { |
| | 0 | 172 | | result.Append(unformattedKey.AsSpan(currentPosition)); |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | return result.ToString().ToLowerInvariant(); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | private string GenerateQrCodeUri(string email, string unformattedKey) |
| | | 179 | | { |
| | 0 | 180 | | return string.Format( |
| | 0 | 181 | | CultureInfo.InvariantCulture, |
| | 0 | 182 | | AuthenticatorUriFormat, |
| | 0 | 183 | | _urlEncoder.Encode("Microsoft.AspNetCore.Identity.UI"), |
| | 0 | 184 | | _urlEncoder.Encode(email), |
| | 0 | 185 | | unformattedKey); |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | } |