共用方式為


作法:設定同盟服務的認證

在 Windows Communication Foundation (WCF) 中,建立同盟服務是由下列主要程序所組成:

  1. 設定 WSFederationHttpBinding 或類似的自訂繫結。 如需建立適當繫結的詳細資訊,請參閱操作說明:建立 WSFederationHttpBinding

  2. 設定 IssuedTokenServiceCredential,此認證可控制如何驗證提供給服務的已發行權杖。

本主題會提供第二個步驟的詳細資訊。 如需同盟服務運作方式的詳細資訊,請參閱同盟

使用程式碼來設定 IssuedTokenServiceCredential 的屬性

  1. 使用 IssuedTokenAuthentication 類別的 ServiceCredentials 屬性,傳回 IssuedTokenServiceCredential 執行個體 (Instance) 的參照。 此屬性可從 Credentials 類別的 ServiceHostBase 屬性存取。

  2. 若要驗證類似 CardSpace 卡片的自我發行權杖,請將 AllowUntrustedRsaIssuers 屬性設定為 true。 預設值為 false

  3. KnownCertificates 屬性所傳回的集合填入 (Populate) X509Certificate2 類別的執行個體。 每個執行個體都代表服務將會從該處驗證權杖的簽發者。

    注意

    不同於 ScopedCertificates 屬性所傳回的用戶端集合,已知憑證集合並不是有索引鍵的集合。 無論傳送包含已發行權杖之訊息的用戶端位址為何,服務都會接受已指定憑證所發行的權杖 (仍有其他條件限制,將於本主題稍後內容中說明)。

  4. CertificateValidationMode 屬性設定為其中一個 X509CertificateValidationMode 列舉值。 只有透過程式碼才能做到這點。 預設值為 ChainTrust

  5. 如果 CertificateValidationMode 屬性是設定為 Custom,則會將自訂 X509CertificateValidator 類別的執行個體指派給 CustomCertificateValidator 屬性。

  6. 如果 CertificateValidationMode 是設定為 ChainTrustPeerOrChainTrust,則會將 RevocationMode 屬性設定為 X509RevocationMode 列舉中的適當值。 請注意,在 PeerTrustCustom 驗證模式中沒有使用撤銷模式。

  7. 如有需要,將自訂 SamlSerializer 類別的執行個體指派給 SamlSerializer 屬性。 例如,需要使用自訂的安全性判斷提示標記語言 (Security Assertions Markup Language,SAML) 序列化程式來剖析自訂 SAML 時。

使用組態來設定 IssuedTokenServiceCredential 的屬性

  1. 建立 <issuedTokenAuthentication> 元素以作為 <serviceCredentials> 元素的子項目。

  2. 若要驗證類似 CardSpace 卡片的自我發行權杖,請將 <issuedTokenAuthentication> 元素的 allowUntrustedRsaIssuers 屬性設定為 true

  3. 建立 <knownCertificates> 項目做為 <issuedTokenAuthentication> 項目的子項。

  4. 建立零個或多個 <add> 項目做為 <knownCertificates> 項目的子項目,並使用 storeLocation, storeNamex509FindTypefindValue 屬性來指定如何找到該憑證。

  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 屬性。 自訂驗證器可以使用其所偏好的任何準則來驗證憑證。 如需詳細資訊,請參閱操作說明:建立使用自訂憑證驗證程式的服務

另請參閱