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.Threading.Tasks;
|
---|
8 | using Microsoft.AspNetCore.Authorization;
|
---|
9 | using Microsoft.AspNetCore.Identity;
|
---|
10 | using Microsoft.AspNetCore.Mvc;
|
---|
11 | using Microsoft.AspNetCore.Mvc.RazorPages;
|
---|
12 | using Microsoft.Extensions.Logging;
|
---|
13 | namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account
|
---|
14 | {
|
---|
15 | public class LoginWithRecoveryCodeModel : PageModel
|
---|
16 | {
|
---|
17 | private readonly SignInManager<IdentityUser> _signInManager;
|
---|
18 | private readonly UserManager<IdentityUser> _userManager;
|
---|
19 | private readonly ILogger<LoginWithRecoveryCodeModel> _logger;
|
---|
20 |
|
---|
21 | public LoginWithRecoveryCodeModel(
|
---|
22 | SignInManager<IdentityUser> signInManager,
|
---|
23 | UserManager<IdentityUser> userManager,
|
---|
24 | ILogger<LoginWithRecoveryCodeModel> logger)
|
---|
25 | {
|
---|
26 | _signInManager = signInManager;
|
---|
27 | _userManager = userManager;
|
---|
28 | _logger = logger;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /// <summary>
|
---|
32 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
33 | /// directly from your code. This API may change or be removed in future releases.
|
---|
34 | /// </summary>
|
---|
35 | [BindProperty]
|
---|
36 | public InputModel Input { get; set; }
|
---|
37 |
|
---|
38 | /// <summary>
|
---|
39 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
40 | /// directly from your code. This API may change or be removed in future releases.
|
---|
41 | /// </summary>
|
---|
42 | public string ReturnUrl { get; set; }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
46 | /// directly from your code. This API may change or be removed in future releases.
|
---|
47 | /// </summary>
|
---|
48 | public class InputModel
|
---|
49 | {
|
---|
50 | /// <summary>
|
---|
51 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
52 | /// directly from your code. This API may change or be removed in future releases.
|
---|
53 | /// </summary>
|
---|
54 | [BindProperty]
|
---|
55 | [Required]
|
---|
56 | [DataType(DataType.Text)]
|
---|
57 | [Display(Name = "Recovery Code")]
|
---|
58 | public string RecoveryCode { get; set; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public async Task<IActionResult> OnGetAsync(string returnUrl = null)
|
---|
62 | {
|
---|
63 | // Ensure the user has gone through the username & password screen first
|
---|
64 | var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
|
---|
65 | if (user == null)
|
---|
66 | {
|
---|
67 | throw new InvalidOperationException($"Unable to load two-factor authentication user.");
|
---|
68 | }
|
---|
69 |
|
---|
70 | ReturnUrl = returnUrl;
|
---|
71 |
|
---|
72 | return Page();
|
---|
73 | }
|
---|
74 |
|
---|
75 | public async Task<IActionResult> OnPostAsync(string returnUrl = null)
|
---|
76 | {
|
---|
77 | if (!ModelState.IsValid)
|
---|
78 | {
|
---|
79 | return Page();
|
---|
80 | }
|
---|
81 |
|
---|
82 | var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
|
---|
83 | if (user == null)
|
---|
84 | {
|
---|
85 | throw new InvalidOperationException($"Unable to load two-factor authentication user.");
|
---|
86 | }
|
---|
87 |
|
---|
88 | var recoveryCode = Input.RecoveryCode.Replace(" ", string.Empty);
|
---|
89 |
|
---|
90 | var result = await _signInManager.TwoFactorRecoveryCodeSignInAsync(recoveryCode);
|
---|
91 |
|
---|
92 | var userId = await _userManager.GetUserIdAsync(user);
|
---|
93 |
|
---|
94 | if (result.Succeeded)
|
---|
95 | {
|
---|
96 | _logger.LogInformation("User with ID '{UserId}' logged in with a recovery code.", user.Id);
|
---|
97 | return LocalRedirect(returnUrl ?? Url.Content("~/"));
|
---|
98 | }
|
---|
99 | if (result.IsLockedOut)
|
---|
100 | {
|
---|
101 | _logger.LogWarning("User account locked out.");
|
---|
102 | return RedirectToPage("./Lockout");
|
---|
103 | }
|
---|
104 | else
|
---|
105 | {
|
---|
106 | _logger.LogWarning("Invalid recovery code entered for user with ID '{UserId}' ", user.Id);
|
---|
107 | ModelState.AddModelError(string.Empty, "Invalid recovery code entered.");
|
---|
108 | return Page();
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|