共用方式為


HOW TO:設定聯合服務的認證

在 Windows Communication Foundation (WCF) 中,建立聯合服務認證的過程包含了下列主要程序:

  1. 設定 WSFederationHttpBinding 或相似的自訂繫結。如需詳細資訊設定適當繫結的詳細資訊,請參閱 HOW TO:建立 WSFederationHttpBinding

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

本主題會提供第二個步驟的詳細資料。如需詳細資訊聯合服務如何運作的詳細資訊,請參閱聯合

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

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

  2. 如果是要驗證類似 CardSpace 卡的自行發行權杖,則將 AllowUntrustedRsaIssuers 屬性設定為 true。預設為 false

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

    ms730131.note(zh-tw,VS.100).gif注意:
    不同於 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 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
// 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;
}

為了讓聯合服務驗證用戶端,下列有關已發行權杖的各項條件必須成立:

  • 當已發行權杖的數位簽章使用 RSA 安全性金鑰識別碼時,AllowUntrustedRsaIssuers 屬性必須是 true

  • 當已發行權杖的簽章使用 X.509 簽發者序號、X.509 主體金鑰識別碼或 X.509 指紋安全性識別碼時,已發行權杖必須由 IssuedTokenServiceCredential 類別之 KnownCertificates 屬性所傳回集合中的憑證完成簽署。

  • 當已發行權杖使用 X.509 憑證完成簽署時,該憑證都必須根據 CertificateValidationMode 屬性值所指定的語意進行驗證,無論該憑證是否當做 X509RawDataKeyIdentifierClause 傳送到信賴憑證者或者是從 KnownCertificates 屬性取得。如需詳細資訊 X.509 憑證驗證的詳細資訊,請參閱使用憑證

例如,將 CertificateValidationMode 設定為 PeerTrust,便會對任何簽署憑證是位於 TrustedPeople 憑證存放區中的憑證進行驗證。在此情況下,請將 TrustedStoreLocation 屬性設定為 CurrentUserLocalMachine。您可以選擇包括 Custom 的其他模式。若是選擇 Custom,您就必須將 X509CertificateValidator 類別的執行個體指派給 CustomCertificateValidator 屬性。自訂驗證器可以使用其所偏好的任何準則來驗證憑證。如需詳細資訊,請參閱 HOW TO:建立使用自訂憑證驗證程式的服務.

另請參閱

工作

聯合範例
HOW TO:在 WSFederationHttpBinding 上停用安全工作階段
HOW TO:建立 WSFederationHttpBinding
HOW TO:建立聯合用戶端

概念

聯合
聯合與信任
使用憑證
SecurityBindingElement 驗證模式