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 ResetAuthenticatorModel : PageModel
|
---|
15 | {
|
---|
16 | private readonly UserManager<IdentityUser> _userManager;
|
---|
17 | private readonly SignInManager<IdentityUser> _signInManager;
|
---|
18 | private readonly ILogger<ResetAuthenticatorModel> _logger;
|
---|
19 |
|
---|
20 | public ResetAuthenticatorModel(
|
---|
21 | UserManager<IdentityUser> userManager,
|
---|
22 | SignInManager<IdentityUser> signInManager,
|
---|
23 | ILogger<ResetAuthenticatorModel> logger)
|
---|
24 | {
|
---|
25 | _userManager = userManager;
|
---|
26 | _signInManager = signInManager;
|
---|
27 | _logger = logger;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /// <summary>
|
---|
31 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
32 | /// directly from your code. This API may change or be removed in future releases.
|
---|
33 | /// </summary>
|
---|
34 | [TempData]
|
---|
35 | public string StatusMessage { get; set; }
|
---|
36 |
|
---|
37 | public async Task<IActionResult> OnGet()
|
---|
38 | {
|
---|
39 | var user = await _userManager.GetUserAsync(User);
|
---|
40 | if (user == null)
|
---|
41 | {
|
---|
42 | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
---|
43 | }
|
---|
44 |
|
---|
45 | return Page();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public async Task<IActionResult> OnPostAsync()
|
---|
49 | {
|
---|
50 | var user = await _userManager.GetUserAsync(User);
|
---|
51 | if (user == null)
|
---|
52 | {
|
---|
53 | return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
|
---|
54 | }
|
---|
55 |
|
---|
56 | await _userManager.SetTwoFactorEnabledAsync(user, false);
|
---|
57 | await _userManager.ResetAuthenticatorKeyAsync(user);
|
---|
58 | var userId = await _userManager.GetUserIdAsync(user);
|
---|
59 | _logger.LogInformation("User with ID '{UserId}' has reset their authentication app key.", user.Id);
|
---|
60 |
|
---|
61 | await _signInManager.RefreshSignInAsync(user);
|
---|
62 | StatusMessage = "Your authenticator app key has been reset, you will need to configure your authenticator app using the new key.";
|
---|
63 |
|
---|
64 | return RedirectToPage("./EnableAuthenticator");
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|