列出 C# 中 System.Security.Claims.ClaimTypes 中存储的所有声明类型

Jun*_*tan 2 c# asp.net system.security claims-based-identity asp.net-identity

使用 Asp.Net Identity 允许您向用户添加声明。System.Security.Claims.ClaimTypes允许您从各种 ClaimType选择任何 ClaimType。

ClaimTypes是一个静态类,定义了可以分配给主题的众所周知的声明类型的常量。

我想将所有这些声明存储在List<>中,并将它们显示在ListBox中,以便具有Admin角色的用户可以在注册后将 ClaimType 分配给用户。

似乎我可以做到这一点,因为ClaimTypes是一个静态类,并且其中定义的那些常量无法列出。

Tra*_*ler 5

您可以通过反映类中的字段来列出声明类型:

var claimTypes = typeof(System.Security.Claims.ClaimTypes).GetFields().ToList();
Run Code Online (Sandbox Code Playgroud)

claimType对于列表中的每个,您可以使用claimType.Name来获取常量名称和claimType.GetValue(null)常量值。