ResetAuthenticator.cshtml.cs 2.5 KB

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