本主题面向不熟悉 Windows® Identity Foundation (WIF) 的 Windows Communication Foundation (WCF) 开发人员,旨在解决以下问题:
哪些操作在 WCF 中有效,但在 WIF 中却不再有效?
WIF 中提供哪些新功能?
如何在 WCF 服务上启用 WIF
在现有 WCF 服务上启用 WIF 的方法有两种:
让您的服务调用 ConfigureServiceHost 或其重载之一。 有关详细信息,请参阅如何以编程方式在 WCF 服务上启用 WIF。
编辑服务的配置文件。 有关详细信息,请参阅如何构建 WCF 信赖方应用程序和使用 FedUtil 建立从 WCF 信赖方服务到 STS 的信任。 具体而言,必须将新行为添加到服务行为列表中,如下面的示例所示。
<behaviors> <serviceBehaviors> <behavior name="ClaimsAwareService1.ServiceBehavior" > <!-- Behavior extension to make the service claims aware --> <federatedServiceHostConfiguration/> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors>
如果单个应用程序上存在多个服务,您可以选择按每个服务启用 WIF。
WIF 符合线路格式标准,因此已启用 WIF 的 WCF 服务与 WCF 3.5 客户端兼容。
针对 WCF 服务开发人员的 Windows Identity Foundation 更改
在现有 WCF 服务上启用 WIF 会导致以下更改:
声明模型
WIF 中的声明模型替换 WCF 中的声明模型。 有关详细信息,请参阅 Claim。
同样地,在 WCF 服务上启用 WIF 时,
ServiceSecurityContext.Current的 Claims 属性将不再可用。 应使用 Thread.CurrentPrincipal 来获取调用者的信息。 但ServiceSecurityContext.Current的其他属性仍然可用。OperationContext 不再包含 WCF 声明。 您应从 Thread.CurrentPrincipal 中获取这些声明。
授权
在 WIF 中,不支持 WCF 策略授权对象。 通常,WCF 开发人员会重写 GetAuthorizationPolicy 以执行声明转换。 在 WCF 中,每个令牌身份验证器都包含策略,而这些策略又包含几组声明。 默认情况下,WCF 会接受所有这些策略。 WCF 中的声明转换包括单独接受或拒绝这些策略。 但是,在 WIF 中,令牌身份验证器由令牌处理程序来代替。 根据新的声明模型,令牌处理程序包含几组声明。 可以在 ClaimsAuthenticationManager 中执行声明转换,即接受或拒绝这些声明。 不再支持重写 GetAuthorizationPolicy,因此,如果原先有声明转换逻辑,应将该逻辑移至自定义 ClaimsAuthenticationManager。
不再支持 WCF 授权策略。 通常,WCF 开发人员会根据 ServiceSecurityContext 中的声明,在插入到服务主机中的自定义授权管理器中做出授权决策。 但是,WIF 会将 WCF 授权策略替换为 ClaimsAuthorizationManager 类。 您应使用用户的 IClaimsPrincipal(可通过调用 Thread.CurrentPrincipal 获得),实现此类的方法并做出授权决策。
WIF 会将默认
ServiceAuthorizationManager替换为其自己的 IdentityModelServiceAuthorizationManager,以便集成到 WCF 宿主环境中。 如果在调用ConfigureServiceHost之前配置ServiceAuthorizationManager,则配置更改将会丢失。 您必须先调用ConfigureServiceHost,然后再对其进行配置。 如果实现自定义ServiceAuthorizationManager,则必须将其更改为从IdentityModelServiceAuthorizationManger继承。 如果在实现时重写GetAuthorizationPolicies,则必须调用base.GetAuthorizationPolicies。ServiceHost.Credentials 中的许多 WCF 身份验证设置都已在 WIF 中弃用。 下面是不受支持的一些设置:
ServiceHost.Credentials.IssuedTokenAuthenticationServiceHost.Credentials.ClientCertificate.AuthenticationServiceHost.Credentials.UserNameAuthentication
杂项
在 WIF 中,可以在服务配置文件的
<system.ServiceModel>/<behaviors>/<serviceBehavior>/<behavior>/<serviceCredentials>部分中指定服务器证书,如下面的示例所示。<serviceCertificate findValue="48BF03FCEDA703DE09E0F1F0CEFED60BB92B3DD8" storeLocation="LocalMachine" storeName="My" x509FindType="FindByThumbprint" />如果同时在 WIF 和 WCF 配置中指定服务器证书,服务将使用在 WIF 配置中指定的证书。 如果仅在 WCF 配置中指定证书,服务将使用该证书。
令牌处理程序是 WCF 令牌提供方、序列化程序、工厂和身份验证器的 WIF 等效项。 要修改令牌的创建、序列化和验证方式,请重写特定令牌处理程序并将自定义处理程序放入到 ServiceConfiguration 上的令牌处理程序集合中。
在 WCF 中,会自动根据
ServiceHost上的终结点来填充受众 URI。 在 WIF 中,必须手动填充 AllowedAudienceUris 属性。对于安全声明标记语言 (SAML) 和 X.509 令牌身份验证,必须实现 IssuerNameRegistry。 有关实现自定义 IssuerNameRegistry 的示例,请参阅 Samples\End-to-end\Authentication Assurance\AuthAssuranceRP\App_Code\TrustedIssuerNameRegistry.cs 文件。 另请参阅如何创建自定义颁发者名称注册表。
许多 WCF 服务配置设置都已在 WIF 中弃用。 在 ServiceConfiguration 类中可以找到其等效项。 下表列出 ServiceConfiguration 中的可用设置以及其在 WCF 中的等效项。
ServiceConfiguration 设置 WCF 等效项 IAuthorizationPolicyServiceAuthorizationManagerPrincipalPermissionAttribute无
IssuedTokenAuthentication.KnownCertificates无
SecurityTokenManagerIssuedTokenAuthentication.AllowedAudienceUris, IssuedTokenAuthentication.AudienceUriModeX509ClientCertificateAuthentication.CertificateValidationMode, IssuedTokenAuthentication.CertificateValidationModeX509ClientCertificateAuthentication.CustomCertificateValidator, IssuedTokenAuthentication.CustomCertificateValidatorX509ClientCertificateAuthentication.RevocationMode, IssuedTokenAuthentication.RevocationModeServiceCredentials.ServiceCertificate无
有关详细信息,请参阅配置。
在 WCF 中,仅针对持有者令牌强制施加 SAML 令牌的受众限制。 在 WIF 中,将对受众进行检查,而不论密钥类型为何。 这会减轻某些种类的令牌转发攻击危害。
针对 WCF 服务开发人员的 Windows Identity Foundation 功能
WIF 添加了几个自定义绑定。 这些绑定旨在供 WCF 客户端在与 Active Directory® Federation Service (AD FS) 2.0 安全令牌服务进行通信时使用。 它们还可以用作自定义 STS 终结点上的绑定。 这些绑定会禁用会话和协商功能,并且不与任何标准的 WCF 绑定相对应,因为可以将它们配置为支持签名 RSA 令牌以允许非对称密钥方案。 有关详细信息,请参阅内置绑定概述。
IClaimsPrinicpal 允许访问进行身份验证的令牌。 此令牌称为引导令牌,是每个 IClaimsPrinicpal 上的一个属性。 默认情况下,此功能为禁用状态,可以通过切换 SaveBootstrapTokens 来启用。
在 ASP.NET 或 WCF 中,可以按下面的示例所示访问引导令牌。
// Get the Bootstrap Token SecurityToken bootstrapToken = null; IClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as IClaimsPrincipal; if ( claimsPrincipal != null ) { IClaimsIdentity claimsIdentity = (IClaimsIdentity)claimsPrincipal.Identity; bootstrapToken = claimsIdentity.BootstrapToken; }