Search code examples
c#asp.net-coreasp.net-core-identity

What's the PersonalDataAttribute good for?


I just stumbled over the fact that ASP.NET Core Identity framework offers a PersonalData attribute. The docs merely say:

Used to indicate that a something is considered personal data.

Okay. What does it mean? Does it have any implication on how the identity frameworks works or what it does? Or is it purely decorative, so that I can do some reflection on some objects and document my code?


Solution

  • The ASP.NET Core Identity UI includes a "Download Personal Data" page, which uses the [PersonalData] attribute to help determine what to include in the download (source):

    // Only include personal data for download
    var personalData = new Dictionary<string, string>();
    var personalDataProps = typeof(TUser).GetProperties().Where(
        prop => Attribute.IsDefined(prop, typeof(PersonalDataAttribute)));
    
    foreach (var p in personalDataProps)
    {
        personalData.Add(p.Name, p.GetValue(user)?.ToString() ?? "null");
    }
    

    There's also [ProtectedPersonalData], which inherits from [PersonalData]. This attribute also configures Identity's EF Core integration to encrypt the property when it's stored in the database.