source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/Manage/DownloadPersonalData.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: 2.4 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.Collections.Generic;
7using System.Linq;
8using System.Text;
9using System.Text.Json;
10using System.Threading.Tasks;
11using Microsoft.AspNetCore.Identity;
12using Microsoft.AspNetCore.Mvc;
13using Microsoft.AspNetCore.Mvc.RazorPages;
14using Microsoft.Extensions.Logging;
15
16namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account.Manage
17{
18 public class DownloadPersonalDataModel : PageModel
19 {
20 private readonly UserManager<IdentityUser> _userManager;
21 private readonly ILogger<DownloadPersonalDataModel> _logger;
22
23 public DownloadPersonalDataModel(
24 UserManager<IdentityUser> userManager,
25 ILogger<DownloadPersonalDataModel> logger)
26 {
27 _userManager = userManager;
28 _logger = logger;
29 }
30
31 public IActionResult OnGet()
32 {
33 return NotFound();
34 }
35
36 public async Task<IActionResult> OnPostAsync()
37 {
38 var user = await _userManager.GetUserAsync(User);
39 if (user == null)
40 {
41 return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
42 }
43
44 _logger.LogInformation("User with ID '{UserId}' asked for their personal data.", _userManager.GetUserId(User));
45
46 // Only include personal data for download
47 var personalData = new Dictionary<string, string>();
48 var personalDataProps = typeof(IdentityUser).GetProperties().Where(
49 prop => Attribute.IsDefined(prop, typeof(PersonalDataAttribute)));
50 foreach (var p in personalDataProps)
51 {
52 personalData.Add(p.Name, p.GetValue(user)?.ToString() ?? "null");
53 }
54
55 var logins = await _userManager.GetLoginsAsync(user);
56 foreach (var l in logins)
57 {
58 personalData.Add($"{l.LoginProvider} external login provider key", l.ProviderKey);
59 }
60
61 personalData.Add($"Authenticator Key", await _userManager.GetAuthenticatorKeyAsync(user));
62
63 Response.Headers.Add("Content-Disposition", "attachment; filename=PersonalData.json");
64 return new FileContentResult(JsonSerializer.SerializeToUtf8Bytes(personalData), "application/json");
65 }
66 }
67}
Note: See TracBrowser for help on using the repository browser.