如何:在联合身份验证服务上配置凭据

在 Windows Communication Foundation (WCF) 中,创建联合服务涉及以下主要过程:

  1. 配置 WSFederationHttpBinding 或类似的自定义绑定。 有关创建适当绑定的详细信息,请参阅如何:创建 WSFederationHttpBinding

  2. 配置 IssuedTokenServiceCredential,它控制如何对提供给服务的已颁发令牌进行身份验证。

本主题提供有关第二个步骤的详细信息。 有关联合服务工作方式的详细信息,请参阅联合

在代码中设置 IssuedTokenServiceCredential 的属性

  1. 使用 IssuedTokenAuthentication 类的 ServiceCredentials 属性返回对 IssuedTokenServiceCredential 实例的引用。 该属性可从 Credentials 类的 ServiceHostBase 属性访问。

  2. 如果要对自行颁发的令牌(比如 CardSpace 卡)进行身份验证,则将 AllowUntrustedRsaIssuers 属性设置为 true。 默认为 false

  3. KnownCertificates 类的实例填充由 X509Certificate2 属性返回的集合。 每个实例表示一个颁发者,服务将从该颁发者对令牌进行身份验证。

    注意

    ScopedCertificates 属性返回的客户端集合不同,已知证书集合不是键控集合。 不管发送包含已颁发令牌的消息的客户端地址是什么,服务都接受指定证书颁发的令牌(还受其他限制,这一点将在本主题的后面说明)。

  4. CertificateValidationMode 属性设置为 X509CertificateValidationMode 枚举值之一。 这只能在代码中完成。 默认为 ChainTrust

  5. 如果 CertificateValidationMode 属性设置为 Custom,则将自定义 X509CertificateValidator 类的一个实例分配给 CustomCertificateValidator 属性。

  6. 如果 CertificateValidationMode 设置为 ChainTrustPeerOrChainTrust,则将 RevocationMode 属性设置为 X509RevocationMode 枚举中的适当值。 请注意,在 PeerTrustCustom 验证模式中不使用吊销模式。

  7. 如果需要,将自定义 SamlSerializer 类的一个实例分配给 SamlSerializer 属性。 例如,如果要分析自定义 SAML 断言,则需要自定义安全断言标记语言 (SAML) 序列化程序。

在配置中设置 IssuedTokenServiceCredential 的属性

  1. 创建一个 <issuedTokenAuthentication> 元素,作为 <serviceCredentials> 元素的子元素。

  2. 如果要验证自行颁发的令牌(比如 CardSpace 卡),则将 <issuedTokenAuthentication> 元素的 allowUntrustedRsaIssuers 属性设置为 true

  3. 创建一个 <knownCertificates> 元素,作为 <issuedTokenAuthentication> 元素的子元素。

  4. 创建零个或多个 <add> 元素作为 <knownCertificates> 元素的子项,并指定如何使用 storeLocationstoreNamex509FindTypefindValue 属性来定位证书。

  5. 必要时,将 <issuedTokenAuthentication> 元素的 samlSerializer 属性设置为自定义 SamlSerializer 类的类型名称。

示例

下面的示例在代码中设置 IssuedTokenServiceCredential 的属性。

// This method configures the IssuedTokenAuthentication property of a ServiceHost.
public static void ConfigureIssuedTokenServiceCredentials(
    ServiceHost sh, bool allowCardspaceTokens, IList<X509Certificate2> knownissuers,
    X509CertificateValidationMode certMode, X509RevocationMode revocationMode, SamlSerializer ser )
{
  // Allow CardSpace tokens.
  sh.Credentials.IssuedTokenAuthentication.AllowUntrustedRsaIssuers = allowCardspaceTokens;

  // Set up known issuer certificates.
  foreach(X509Certificate2 cert in knownissuers)
    sh.Credentials.IssuedTokenAuthentication.KnownCertificates.Add ( cert );

  // Set issuer certificate validation and revocation checking modes.
  sh.Credentials.IssuedTokenAuthentication.CertificateValidationMode =
      X509CertificateValidationMode.PeerOrChainTrust;
  sh.Credentials.IssuedTokenAuthentication.RevocationMode = X509RevocationMode.Online;
  sh.Credentials.IssuedTokenAuthentication.TrustedStoreLocation = StoreLocation.LocalMachine;

  // Set the SamlSerializer, if one is specified.
  if ( ser != null )
    sh.Credentials.IssuedTokenAuthentication.SamlSerializer = ser;
}
' This method configures the IssuedTokenAuthentication property of a ServiceHost.
Public Shared Sub ConfigureIssuedTokenServiceCredentials( _
    ByVal sh As ServiceHost, _
    ByVal allowCardspaceTokens As Boolean, _
    ByVal knownissuers As IList(Of X509Certificate2), _
    ByVal certMode As X509CertificateValidationMode, _
    ByVal revocationMode As X509RevocationMode, _
    ByVal ser As SamlSerializer _
    )
    ' Allow CardSpace tokens.
    sh.Credentials.IssuedTokenAuthentication.AllowUntrustedRsaIssuers = _
    allowCardspaceTokens

    ' Set up known issuer certificates.
    Dim cert As X509Certificate2
    For Each cert In knownissuers
        sh.Credentials.IssuedTokenAuthentication.KnownCertificates.Add(cert)
    Next cert
    ' Set issuer certificate validation and revocation checking modes.
    sh.Credentials.IssuedTokenAuthentication.CertificateValidationMode = _
        X509CertificateValidationMode.PeerOrChainTrust
    sh.Credentials.IssuedTokenAuthentication.RevocationMode = _
    X509RevocationMode.Online

    ' Set the SamlSerializer, if one is specified.
    If Not (ser Is Nothing) Then
        sh.Credentials.IssuedTokenAuthentication.SamlSerializer = ser
    End If
End Sub

为了使联合服务能够对客户端进行身份验证,必须满足有关已颁发令牌的下列各项条件:

例如,将 CertificateValidationMode 设置为 PeerTrust 将对签名证书位于 TrustedPeople 证书存储中的任何已颁发令牌进行身份验证。 在这种情况下,请将 TrustedStoreLocation 属性设置为 CurrentUserLocalMachine。 您可以选择其他模式,包括 Custom。 如果选择了 Custom,则必须将 X509CertificateValidator 类的一个实例分配给 CustomCertificateValidator 属性。 自定义验证程序可以使用它喜欢的任何条件来验证证书。 有关详细信息,请参见如何:创建使用自定义证书验证程序的服务

另请参阅