语言

如何创建 WSFederationHttpBinding

在 Windows Communication Foundation (WCF) 中,WSFederationHttpBinding 类(配置中的 <wsFederationHttpBinding>)提供了一种用于公开联合服务的机制。 即,服务要求客户端使用安全令牌服务颁发的安全令牌进行身份验证。 本主题演示如何通过代码和配置来设置 WSFederationHttpBinding。 绑定一经创建,你就可以设置一个终结点来使用该绑定。

基本步骤概述如下:

  1. 选择一种安全模式。 WSFederationHttpBinding 支持 Message,它在消息级提供端对端的安全(即使跨多个跃点也是如此),还支持 TransportWithMessageCredential,它在客户端和服务可以通过 HTTPS 直接连接的情况下提供较好的性能。

    注意

    WSFederationHttpBinding 还支持 None 作为安全模式。 此模式不安全,仅用于调试目的。 如果服务终结点是利用安全模式设置为 WSFederationHttpBinding 的 None 来部署的,则得到的客户端绑定(由 ServiceModel 元数据实用工具 (Svcutil.exe) 生成)为 WSHttpBinding,其安全模式为 None。

    与其他系统提供的绑定不同,使用 WSFederationHttpBinding 时无需选择客户端凭据类型。 这是因为客户端凭据类型始终为已颁发令牌。 WCF 从指定的颁发者获取一个令牌,并将该令牌提供给服务以对客户端进行身份验证。

  2. 在联合客户端上,将 IssuerAddress 属性设置为安全令牌服务的 URL。 将 IssuerBinding 设置为要使用的绑定,以便与安全令牌服务进行通信。

  3. 可选。 将 IssuedTokenType 属性设置为令牌类型的统一资源标识符 (URI)。 在联合服务上,指定服务期望使用的令牌类型。 在联合客户端上,指定客户端向安全令牌服务请求的令牌类型。

    如果未指定令牌类型,则默认情况下,客户端将在无令牌类型 URI 的情况下生成 WS-Trust 请求安全令牌 (RST),而服务希望使用安全断言标记语言 (SAML) 1.1 令牌进行客户端身份验证。

    SAML 1.1 令牌的 URI 是 http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1。

  4. 可选。 在联合服务上,将 IssuerMetadataAddress 属性设置为安全令牌服务的元数据 URL。 使用元数据终结点,服务的客户端可以选择一个适合的绑定/终结点对,前提是将服务配置为发布元数据。 有关发布元数据的详细信息,请参阅发布元数据。

你还可以设置其他属性,包括已颁发令牌中用作校验密钥的密钥类型、在客户端和服务之间使用的算法套件、是协商还是显式指定服务凭据、服务期望已颁发令牌中包含的任何特定声明,以及必须添加到由客户端发送到安全令牌服务的请求的其他 XML 元素。

注意

仅当 NegotiateServiceCredential 设置为 SecurityMode 时,Message 属性才是相关的。 如果将 SecurityMode 设置为 TransportWithMessageCredential,则将忽略 NegotiateServiceCredential 属性。

以代码方式配置 WSFederationHttpBinding

  1. 创建 WSFederationHttpBinding 的实例:

  2. 根据需要将 Mode 属性设置为 WSFederationHttpSecurityMode 或 Message。 如果需要非 Basic256 的算法套件,请将 AlgorithmSuite 属性设置为从 SecurityAlgorithmSuite 中获取的值。

  3. 根据需要设置 NegotiateServiceCredential 属性。

  4. 根据需要将 IssuedKeyType 属性设置为 SecurityKeyTypeSymmetricKey 或。 AsymmetricKey根据需要。

  5. 将 IssuedTokenType 属性设置为适当的值。 如果未设置任何值,则 WCF 默认为 http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1,这表示 SAML 1.1 令牌。

  6. 如果未指定本地颁发者则在客户端上是必需的;在服务上是可选的。 创建一个包含安全令牌服务的地址和标识信息的 EndpointAddress,然后将 EndpointAddress 实例分配给 IssuerAddress 属性。

  7. 如果未指定本地颁发者则在客户端上是必需的;不能在服务上使用。 为 Binding 创建一个 SecurityTokenService,并将 Binding 实例分配给 IssuerBinding 属性。

  8. 不能在客户端上使用;在服务上可选。 为安全令牌服务的元数据创建一个 EndpointAddress 实例,然后将其分配给 IssuerMetadataAddress 属性。

  9. 在客户端和服务上都是可选的。 创建一个或多个 ClaimTypeRequirement 实例,并将其添加到 ClaimTypeRequirements 属性返回的集合中。

  10. 在客户端和服务上都是可选的。 创建一个或多个 XmlElement 实例,并将其添加到 TokenRequestParameters 属性返回的集合中。

在配置中创建联合终结点

  1. 在应用程序配置文件中,创建一个 <wsFederationHttpBinding> 作为 <bindings> 元素的子元素。

  2. 创建一个 <binding> 元素作为 <wsFederationHttpBinding> 的子元素,并将 name 特性设置为适当的值。

  3. 创建一个 <security> 元素作为 <binding> 元素的子元素。

  4. 根据需要将 mode 元素的 <security> 属性设置为 Message 或 TransportWithMessageCredential 的一个值。

  5. 创建一个 <message> 元素,作为 <security> 元素的子元素。

  6. 可选。 将 algorithmSuite 元素的 <message> 属性设置为适当的值。 默认值为 Basic256。

  7. 可选。 如果需要非对称校验密钥,请将 issuedKeyType 元素的 <message> 属性设置为 AsymmetricKey。 默认值为 SymmetricKey。

  8. 可选。 设置 issuedTokenType 元素的 <message> 属性。

  9. 如果未指定本地颁发者则在客户端上是必需的;在服务上是可选的。 创建一个 <issuer> 元素,作为 <message> 元素的子元素。

  10. 将 address 属性设置为 <issuer> 元素,并指定安全令牌服务接受令牌请求的地址。

  11. 可选。 添加一个 <identity> 子元素,并指定安全令牌服务的标识。

  12. 有关详细信息,请参阅服务标识和身份验证。

  13. 如果未指定本地颁发者则在客户端上是必需的;不能在服务上使用。 在可以用于与安全令牌服务进行通信的绑定节中创建一个 <binding> 元素。 有关创建绑定的详细信息,请参阅如何:在配置中指定服务绑定。

  14. 通过设置 binding 元素的 bindingConfiguration 和 <issuer> 属性,来指定在上一步创建的绑定。

  15. 不能在客户端上使用;在服务上可选。 创建一个 <issuerMetadata> 元素,作为 <message> 元素的子元素。 然后,在 address 元素的 <issuerMetadata> 属性中,指定安全令牌服务将要发布其元数据的地址。 还可以选择添加一个 <identity> 子元素,并指定安全令牌服务的标识。

  16. 在客户端和服务上都是可选的。 添加一个 <claimTypeRequirements> 元素,将其作为 <message> 元素的子元素。 通过将 <add> 元素添加到 <claimTypeRequirements> 元素并利用 claimType 属性指定声明类型,指定服务所依赖的必选和可选声明。 通过设置 isOptional 属性来指定给定的声明是必选的还是可选的。

示例

下面的代码示例演示如何以命令式编程方式设置 WSFederationHttpBinding。

// This method creates a WSFederationHttpBinding.
public static WSFederationHttpBinding
    CreateWSFederationHttpBinding(bool isClient)
{
  // Create an instance of the WSFederationHttpBinding.
  WSFederationHttpBinding b = new WSFederationHttpBinding();

  // Set the security mode to Message.
  b.Security.Mode = WSFederationHttpSecurityMode.Message;

  // Set the Algorithm Suite to Basic256Rsa15.
  b.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Basic256Rsa15;

  // Set NegotiateServiceCredential to true.
  b.Security.Message.NegotiateServiceCredential = true;

  // Set IssuedKeyType to Symmetric.
  b.Security.Message.IssuedKeyType = SecurityKeyType.SymmetricKey;

  // Set IssuedTokenType to SAML 1.1
  b.Security.Message.IssuedTokenType =
      "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#samlv1.1";
    
  // Extract the STS certificate from the certificate store.
  X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
  store.Open(OpenFlags.ReadOnly);
  X509Certificate2Collection certs = store.Certificates.Find(
      X509FindType.FindByThumbprint, "0000000000000000000000000000000000000000", false);
  store.Close();

  // Create an EndpointIdentity from the STS certificate.
  EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity ( certs[0] );

  // Set the IssuerAddress using the address of the STS and the previously created
  // EndpointIdentity.
  b.Security.Message.IssuerAddress =
      new EndpointAddress(new Uri("http://localhost:8000/sts/x509"), identity);

  // Set the IssuerBinding to a WSHttpBinding loaded from configuration.
  // The IssuerBinding is only used on federated clients.
  if (isClient)
  {
      b.Security.Message.IssuerBinding = new WSHttpBinding("Issuer");
  }

  // Set the IssuerMetadataAddress using the metadata address of the STS and the
  // previously created EndpointIdentity. The IssuerMetadataAddress is only used
  // on federated services.
  else
  {
      b.Security.Message.IssuerMetadataAddress =
          new EndpointAddress(new Uri("http://localhost:8001/sts/mex"), identity);
  }
  // Create a ClaimTypeRequirement.
  ClaimTypeRequirement ctr = new ClaimTypeRequirement
      ("http://example.org/claim/c1", false);

  // Add the ClaimTypeRequirement to ClaimTypeRequirements
  b.Security.Message.ClaimTypeRequirements.Add(ctr);

  // Return the created binding
  return b;
}
' This method creates a WSFederationHttpBinding.
Public Shared Function CreateWSFederationHttpBinding(ByVal isClient As Boolean) As WSFederationHttpBinding
    ' Create an instance of the WSFederationHttpBinding.
    Dim b As New WSFederationHttpBinding()
    With b.Security
        ' Set the security mode to Message.
        .Mode = WSFederationHttpSecurityMode.Message

        With .Message
            ' Set the Algorithm Suite to Basic256Rsa15.
            .AlgorithmSuite = SecurityAlgorithmSuite.Basic256Rsa15

            ' Set NegotiateServiceCredential to true.
            .NegotiateServiceCredential = True

            ' Set IssuedKeyType to Symmetric.
            .IssuedKeyType = SecurityKeyType.SymmetricKey

            ' Set IssuedTokenType to SAML 1.1
            .IssuedTokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#samlv1.1"
        End With
    End With

    ' Extract the STS certificate from the certificate store.
    Dim store As New X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser)
    store.Open(OpenFlags.ReadOnly)
    Dim certs = store.Certificates.Find(X509FindType.FindByThumbprint, _
                                        "0000000000000000000000000000000000000000", _
                                        False)
    store.Close()

    ' Create an EndpointIdentity from the STS certificate.
    Dim identity = EndpointIdentity.CreateX509CertificateIdentity(certs(0))

    ' Set the IssuerAddress using the address of the STS and the previously created 
    ' EndpointIdentity.
    With b.Security.Message
        .IssuerAddress = New EndpointAddress(New Uri("http://localhost:8000/sts/x509"), _
                                                                           identity)

        ' Set the IssuerBinding to a WSHttpBinding loaded from configuration. 
        ' The IssuerBinding is only used on federated clients.
        If isClient Then
            .IssuerBinding = New WSHttpBinding("Issuer")

            ' Set the IssuerMetadataAddress using the metadata address of the STS and the
            ' previously created EndpointIdentity. The IssuerMetadataAddress is only used 
            ' on federated services.
        Else
            .IssuerMetadataAddress = New EndpointAddress(New Uri("http://localhost:8001/sts/mex"), _
                                                                           identity)
        End If
        ' Create a ClaimTypeRequirement.
        Dim ctr As New ClaimTypeRequirement("http://example.org/claim/c1", _
                                            False)

        ' Add the ClaimTypeRequirement to ClaimTypeRequirements
        .ClaimTypeRequirements.Add(ctr)
    End With

    ' Return the created binding
    Return b
End Function

另请参阅