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.Threading.Tasks;
|
---|
7 | using Microsoft.AspNetCore.Identity;
|
---|
8 | using Microsoft.AspNetCore.Mvc;
|
---|
9 | using Microsoft.AspNetCore.Mvc.RazorPages;
|
---|
10 | using Microsoft.Extensions.Logging;
|
---|
11 |
|
---|
12 | namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
|
---|
13 | {
|
---|
14 | public class TwoFactorAuthenticationModel : PageModel
|
---|
15 | {
|
---|
16 | private readonly UserManager<IdentityUser> _userManager;
|
---|
17 | private readonly SignInManager<IdentityUser> _signInManager;
|
---|
18 | private readonly ILogger<TwoFactorAuthenticationModel> _logger;
|
---|
19 |
|
---|
20 | public TwoFactorAuthenticationModel(
|
---|
21 | UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, ILogger<TwoFactorAuthenticationModel> logger)
|
---|
22 | {
|
---|
23 | _userManager = userManager;
|
---|
24 | _signInManager = signInManager;
|
---|
25 | _logger = logger;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
30 | /// directly from your code. This API may change or be removed in future releases.
|
---|
31 | /// </summary>
|
---|
32 | public bool HasAuthenticator { get; set; }
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
36 | /// directly from your code. This API may change or be removed in future releases.
|
---|
37 | /// </summary>
|
---|
38 | public int RecoveryCodesLeft { get; set; }
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
42 | /// directly from your code. This API may change or be removed in future releases.
|
---|
43 | /// </summary>
|
---|
44 | [BindProperty]
|
---|
45 | public bool Is2faEnabled { get; set; }
|
---|
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 | public bool IsMachineRemembered { get; set; }
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
55 | /// directly from your code. This API may change or be removed in future releases.
|
---|
56 | /// </summary>
|
---|
57 | [TempData]
|
---|
58 | public string StatusMessage { get; set; }
|
---|
59 |
|
---|
60 | public async Task<IActionResult> OnGetAsync()
|
---|
61 | {
|
---|
62 | var user = await _userManager.GetUserAsync(User);
|
---|
63 | if (user == null)
|
---|
64 | {
|
---|
65 | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
---|
66 | }
|
---|
67 |
|
---|
68 | HasAuthenticator = await _userManager.GetAuthenticatorKeyAsync(user) != null;
|
---|
69 | Is2faEnabled = await _userManager.GetTwoFactorEnabledAsync(user);
|
---|
70 | IsMachineRemembered = await _signInManager.IsTwoFactorClientRememberedAsync(user);
|
---|
71 | RecoveryCodesLeft = await _userManager.CountRecoveryCodesAsync(user);
|
---|
72 |
|
---|
73 | return Page();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public async Task<IActionResult> OnPostAsync()
|
---|
77 | {
|
---|
78 | var user = await _userManager.GetUserAsync(User);
|
---|
79 | if (user == null)
|
---|
80 | {
|
---|
81 | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
---|
82 | }
|
---|
83 |
|
---|
84 | await _signInManager.ForgetTwoFactorClientAsync();
|
---|
85 | StatusMessage = "The current browser has been forgotten. When you login again from this browser you will be prompted for your 2fa code.";
|
---|
86 | return RedirectToPage();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|