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 |
|
---|
5 | using System;
|
---|
6 | using System.Collections.Generic;
|
---|
7 | using System.Linq;
|
---|
8 | using System.Text;
|
---|
9 | using System.Text.Json;
|
---|
10 | using System.Threading.Tasks;
|
---|
11 | using Microsoft.AspNetCore.Identity;
|
---|
12 | using Microsoft.AspNetCore.Mvc;
|
---|
13 | using Microsoft.AspNetCore.Mvc.RazorPages;
|
---|
14 | using Microsoft.Extensions.Logging;
|
---|
15 |
|
---|
16 | namespace 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 | }
|
---|