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

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

使用 Asp.Net 身份允许您向用户添加声明。 System.Security.Claims.ClaimTypes 允许您从各种 ClaimTypes 中 select 任何 ClaimType。

ClaimTypes 是静态的 class 并为可以分配给主题的众所周知的声明类型定义常量。

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

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

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

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

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