DeletePersonalData.cshtml.cs 3.9 KB

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