ASP.NET 和 Windows Communication Foundation (WCF) 开发人员通常使用 IIdentity 和 IPrincipal 接口处理用户的标识信息。 .NET 4.5 中集成了 Windows Identity Foundation (WIF),因此对于任何主体,声明都将始终存在,如下图所示:
在 .NET 4.5 中,System.Security.Claims 包含新的 ClaimsPrincipal 和 ClaimsIdentity 类(如上图所示)。 .NET 中的所有主体现在均派生自 ClaimsPrincipal。 所有内置标识类(如 ASP.NET 的 WindowsIdentity 和 FormsIdentity)现在都派生自 ClaimsIdentity。 同样,所有内置主体类(如 GenericPrincipal 和 WindowsPrincipal)都派生自 ClaimsPrincipal。
声明由 Claim 类表示。 此类具有以下重要属性:
- Type 表示声明的类型,通常是一个 URI。 例如,电子邮件地址声明表示为
http://schemas.microsoft.com/ws/2008/06/identity/claims/email
。 - Value 包含声明的值,表现形式为字符串。 例如,电子邮件地址可以表示为"someone@contoso.com"。
- ValueType 表示声明值的类型,通常是一个 URI。 例如,字符串类型表示为
http://www.w3.org/2001/XMLSchema#string
。 根据 XML 架构,该值类型必须为 QName。 为使 WIF 输出有效的 QName 值,值的格式必须为namespace#format
。 如果命名空间不是定义完善的命名空间,生成的 XML 可能无法进行架构验证,因为没有为该命名空间发布的 XSD 文件。 默认值类型为http://www.w3.org/2001/XMLSchema#string
。 有关可安全使用的已知值类型的信息,请参阅W3C XML 架构页。 - Issuer 是颁发声明的安全令牌服务 (STS) 标识符。 这可以表示为 STS 或者代表 STS名称的 URL ,例如
https://sts1.contoso.com/sts
。 - 无论链中有多少个 STS,OriginalIssuer 是最初颁发声明的 STS 的标识符。 它的表示形式与 Issuer 类似。
- Subject 是标识正在被检查的使用者。 该方法包含 ClaimsIdentity。
- Properties 是一个字典,开发人员可以使用它来提供应用程序特定的数据,以便与其他属性一起在线传送。此外,它还可用于自定义验证。
标识委托
ClaimsIdentity 的重要属性之一是 Acktor。 使用此属性可在多层系统中进行凭据委派(中间层充当客户端的角色,负责向后端服务提出请求)。
通过 Thread.CurrentPrincipal 访问声明
若要在声明感知应用程序中访问当前用户的声明集,请使用 Thread.CurrentPrincipal
。
下面的代码示例说明如何使用此方法获取 System.Security.Claims.ClaimsIdentity:
C#复制
ClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal;
角色声明类型
配置声明感知应用程序的过程中,应确定使用的角色声明类型。 此声明类型由 System.Security.Claims.ClaimsPrincipal.IsInRole (System.String) 使用。 默认声明类型为 http://schemas.microsoft.com/ws/2008/06/identity/claims/role
。
Windows Identity Foundation 从不同令牌类型中提取的声明
WIF 支持开箱即用的几种身份验证机制组合。 下表列出了 WIF 从不同令牌类型中提取的声明。
标记类型 | 生成的声明 | 映射到 Windows 访问令牌 |
SAML 1.1 | 1.System.IdentityModel.SecurityTokenService.GetOutputClaimsIdentity (System.Security.Claims.ClaimsPrincipal,System.IdentityModel.Protocols.WSTrust.RequestSecurityToken,System.IdentityModel.Scope) 中的所有声明。 2.包含确认密钥的 XML 序列化的 | 除了“SAML 1.1”中列出的声明(不包括 |
SAML 2.0 | 与“SAML 1.1”相同。 | 与“映射到 Windows 帐户的 SAML 1.1”相同。 |
X509 | 1.具有 X500 可分辨名称、emailName、dnsName、SimpleName、UpnName、UrlName、thumbprint、RsaKey(可以使用 RSACryptoServiceProvider.ExportParameters 方法从 X509Certificate2.PublicKey.Key 属性提取)、DsaKey(可以使用 DSACryptoServiceProvider.ExportParameters 方法从 X509Certificate2.PublicKey.Key 属性提取)和 X509 证书中 SerialNumber 属性的声明。 2.值为 | 1.它使用 Windows 帐户完全限定的域名作为 |
UPN | 1.与 Windows 身份验证部分中的声明类似的声明。 2.值为 | |
Windows(Kerberos 或 NTLM) | 1.从访问令牌,如生成的声明:PrimarySID、 DenyOnlyPrimarySID、 PrimaryGroupSID、 DenyOnlyPrimaryGroupSID、 GroupSID、 DenyOnlySID 和名称 2.值为 | |
RSA 密钥对 | 1.值为 RSAKeyValue 的 |
身份验证类型 | “AuthenticationMethod”声明中发出的 URI |
Password |
|
Kerberos |
|
SecureRemotePassword |
|
TLSClient |
|
X509 |
|
PGP |
|
Spki |
|
XmlDSig |
|
未指定 |
|