source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/Manage/DeletePersonalData.cshtml.cs

main
Last change on this file was 2aea0fd, checked in by ElenaMoskova <elena.moskova99@…>, 4 months ago

init commit Elena

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