Search code examples
c#asp.netasp.net-identityclaims-based-identitysystem.security

List all the Claim types stored in the System.Security.Claims.ClaimTypes in C#


Working with Asp.Net Identity allow you to add the claims to a user. And the System.Security.Claims.ClaimTypes allows you to select any ClaimType from various ClaimTypes.

ClaimTypes is a static class and defines constants for well-known claim types that can be assigned to subjects.

I want to store all these claims in a List<> and display them in a ListBox so that the user with the Admin role can assign a ClaimType to a user after registration.

Seems that I can do that as ClaimTypes is a static class and those constants defined in that can't be listed.


Solution

  • You can list out the claim types by reflecting on the fields in the class:

    var claimTypes = typeof(System.Security.Claims.ClaimTypes).GetFields().ToList();
    

    For each claimType in the list, you can use claimType.Name to get the constant name and claimType.GetValue(null) to get the constant value.