DeletePersonalData.cshtml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.ComponentModel.DataAnnotations;
  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. namespace bitforum.Areas.Identity.Pages.Account.Manage
  12. {
  13. public class DeletePersonalDataModel : PageModel
  14. {
  15. private readonly UserManager<IdentityUser> _userManager;
  16. private readonly SignInManager<IdentityUser> _signInManager;
  17. private readonly ILogger<DeletePersonalDataModel> _logger;
  18. public DeletePersonalDataModel(
  19. UserManager<IdentityUser> userManager,
  20. SignInManager<IdentityUser> signInManager,
  21. ILogger<DeletePersonalDataModel> logger)
  22. {
  23. _userManager = userManager;
  24. _signInManager = signInManager;
  25. _logger = logger;
  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. [BindProperty]
  32. public InputModel Input { get; set; }
  33. /// <summary>
  34. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  35. /// directly from your code. This API may change or be removed in future releases.
  36. /// </summary>
  37. public class InputModel
  38. {
  39. /// <summary>
  40. /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
  41. /// directly from your code. This API may change or be removed in future releases.
  42. /// </summary>
  43. [Required]
  44. [DataType(DataType.Password)]
  45. public string Password { 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 RequirePassword { get; set; }
  52. public async Task<IActionResult> OnGet()
  53. {
  54. var user = await _userManager.GetUserAsync(User);
  55. if (user == null)
  56. {
  57. return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  58. }
  59. RequirePassword = await _userManager.HasPasswordAsync(user);
  60. return Page();
  61. }
  62. public async Task<IActionResult> OnPostAsync()
  63. {
  64. var user = await _userManager.GetUserAsync(User);
  65. if (user == null)
  66. {
  67. return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
  68. }
  69. RequirePassword = await _userManager.HasPasswordAsync(user);
  70. if (RequirePassword)
  71. {
  72. if (!await _userManager.CheckPasswordAsync(user, Input.Password))
  73. {
  74. ModelState.AddModelError(string.Empty, "Incorrect password.");
  75. return Page();
  76. }
  77. }
  78. var result = await _userManager.DeleteAsync(user);
  79. var userId = await _userManager.GetUserIdAsync(user);
  80. if (!result.Succeeded)
  81. {
  82. throw new InvalidOperationException($"Unexpected error occurred deleting user.");
  83. }
  84. await _signInManager.SignOutAsync();
  85. _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId);
  86. return Redirect("~/");
  87. }
  88. }
  89. }