source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/Manage/Disable2fa.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: 2.5 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.Threading.Tasks;
7using Microsoft.AspNetCore.Identity;
8using Microsoft.AspNetCore.Mvc;
9using Microsoft.AspNetCore.Mvc.RazorPages;
10using Microsoft.Extensions.Logging;
11
12namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
13{
14 public class Disable2faModel : PageModel
15 {
16 private readonly UserManager<IdentityUser> _userManager;
17 private readonly ILogger<Disable2faModel> _logger;
18
19 public Disable2faModel(
20 UserManager<IdentityUser> userManager,
21 ILogger<Disable2faModel> logger)
22 {
23 _userManager = userManager;
24 _logger = logger;
25 }
26
27 /// <summary>
28 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
29 /// directly from your code. This API may change or be removed in future releases.
30 /// </summary>
31 [TempData]
32 public string StatusMessage { get; set; }
33
34 public async Task<IActionResult> OnGet()
35 {
36 var user = await _userManager.GetUserAsync(User);
37 if (user == null)
38 {
39 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
40 }
41
42 if (!await _userManager.GetTwoFactorEnabledAsync(user))
43 {
44 throw new InvalidOperationException($"Cannot disable 2FA for user as it's not currently enabled.");
45 }
46
47 return Page();
48 }
49
50 public async Task<IActionResult> OnPostAsync()
51 {
52 var user = await _userManager.GetUserAsync(User);
53 if (user == null)
54 {
55 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
56 }
57
58 var disable2faResult = await _userManager.SetTwoFactorEnabledAsync(user, false);
59 if (!disable2faResult.Succeeded)
60 {
61 throw new InvalidOperationException($"Unexpected error occurred disabling 2FA.");
62 }
63
64 _logger.LogInformation("User with ID '{UserId}' has disabled 2fa.", _userManager.GetUserId(User));
65 StatusMessage = "2fa has been disabled. You can reenable 2fa when you setup an authenticator app";
66 return RedirectToPage("./TwoFactorAuthentication");
67 }
68 }
69}
Note: See TracBrowser for help on using the repository browser.