source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml.cs

main
Last change on this file was 2aea0fd, checked in by ElenaMoskova <elena.moskova99@…>, 2 months ago

init commit Elena

  • Property mode set to 100644
File size: 3.1 KB
Line 
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.Linq;
7using System.Threading.Tasks;
8using Microsoft.AspNetCore.Identity;
9using Microsoft.AspNetCore.Mvc;
10using Microsoft.AspNetCore.Mvc.RazorPages;
11using Microsoft.Extensions.Logging;
12
13namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
14{
15 public class GenerateRecoveryCodesModel : PageModel
16 {
17 private readonly UserManager<IdentityUser> _userManager;
18 private readonly ILogger<GenerateRecoveryCodesModel> _logger;
19
20 public GenerateRecoveryCodesModel(
21 UserManager<IdentityUser> userManager,
22 ILogger<GenerateRecoveryCodesModel> logger)
23 {
24 _userManager = userManager;
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 [TempData]
33 public string[] RecoveryCodes { get; set; }
34
35 /// <summary>
36 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
37 /// directly from your code. This API may change or be removed in future releases.
38 /// </summary>
39 [TempData]
40 public string StatusMessage { get; set; }
41
42 public async Task<IActionResult> OnGetAsync()
43 {
44 var user = await _userManager.GetUserAsync(User);
45 if (user == null)
46 {
47 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
48 }
49
50 var isTwoFactorEnabled = await _userManager.GetTwoFactorEnabledAsync(user);
51 if (!isTwoFactorEnabled)
52 {
53 throw new InvalidOperationException($"Cannot generate recovery codes for user because they do not have 2FA enabled.");
54 }
55
56 return Page();
57 }
58
59 public async Task<IActionResult> OnPostAsync()
60 {
61 var user = await _userManager.GetUserAsync(User);
62 if (user == null)
63 {
64 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
65 }
66
67 var isTwoFactorEnabled = await _userManager.GetTwoFactorEnabledAsync(user);
68 var userId = await _userManager.GetUserIdAsync(user);
69 if (!isTwoFactorEnabled)
70 {
71 throw new InvalidOperationException($"Cannot generate recovery codes for user as they do not have 2FA enabled.");
72 }
73
74 var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
75 RecoveryCodes = recoveryCodes.ToArray();
76
77 _logger.LogInformation("User with ID '{UserId}' has generated new 2FA recovery codes.", userId);
78 StatusMessage = "You have generated new recovery codes.";
79 return RedirectToPage("./ShowRecoveryCodes");
80 }
81 }
82}
Note: See TracBrowser for help on using the repository browser.