HOW TO:指定用戶端認證值

您可以使用 Windows Communication Foundation (WCF) 來指定如何對服務驗證用戶端。例如,您可以規定向服務驗證用戶端時必須使用憑證。這個範例會將安全性模式設定為傳輸,並將用戶端認證類型設定為憑證。

此程序會在三個部分中執行:

  1. 服務開發人員指定用戶端必須提供的「認證類型」(Credential Type)。
  2. 用戶端開發人員使用 ServiceModel Metadata Utility Tool (Svcutil.exe) 來查詢服務的中繼資料 (服務用戶端程式碼與組態檔)。
  3. 用戶端開發人員建立用戶端應用程式,並在程式碼或組態中提供符合認證類型的「認證值」(Credential Value),如中繼資料所指定。

服務程式碼

若要透過程式碼指定服務的用戶端認證類型

  1. 將下列參照新增至您的程式碼中。

  2. 建立繫結的執行個體。這個範例會使用 NetTcpBinding

  3. 必要時設定安全性模式。這個範例會將模式設定為 Transport

  4. 必要時,將 ClientCredentialType 設定為認證。這個範例會將類型設定為憑證。

  5. 在建構函式中使用您的服務類型來建立 ServiceHost 的執行個體。為求簡單扼要,此處將不說明用以顯示服務介面及其實作的程式碼。

  6. 設定服務的認證值。這個範例會使用 X509CertificateRecipientServiceCredential 類別的 SetCertificate 方法。

若要透過組態指定服務的用戶端認證類型

  1. 將適當的繫結新增至 <bindings> 項目。這個範例會使用 <netTcpBinding Element>

  2. <security> of <netTcpBinding> 新增至繫結。請務必將必要的 name 屬性設定為適當值。

  3. mode 屬性設定為適當值。這個範例會使用 "Message" 模式。

  4. 必要時,將傳輸或訊息項目新增至安全性繫結。這個範例會使用 <message> element of <netTcpBinding>

  5. clientCredential 屬性設定為所需的認證類型。這個範例會使用 "Certificate"

    <bindings>
      <netTcpBinding>
        <binding name="TcpBindingWithCredential">
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    
  6. 建立端點項目時,使用 bindingConfiguration 屬性值來指定繫結組態,如下列程式碼所示。

    <endpoint address="myEndpointAddress"
              binding="netTcpBinding"
              bindingConfiguration="TcpBindingWithCredential"
                 name="netTcpBinding_ICalculator"
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
    

用戶端程式碼

下列程序示範如何透過程式碼與組態,設定用戶端的用戶端認證值。這項程序假定您已經使用 ServiceModel Metadata Utility Tool (Svcutil.exe) 從服務傳回中繼資料 (程式碼與組態)。如需詳細資訊,請參閱 HOW TO:建立 Windows Communication Foundation 用戶端

若要透過程式碼指定用戶端的用戶端認證值

  1. 使用 ServiceModel Metadata Utility Tool (Svcutil.exe) 從服務中產生程式碼與組態。

  2. 使用產生的程式碼來建立 WCF 用戶端的執行個體。

  3. 在用戶端類別上,將 ClientBase 類別的 ClientCredentials 屬性設定為適當值。這個範例會使用 X509CertificateInitiatorClientCredential 類別的 SetCertificate 方法,將屬性設定為 X.509 憑證。

    您可以使用 X509FindType 類別的任何一種列舉。此處將使用主旨名稱,以避免憑證因為到期日關係而變更。使用主旨名稱可讓基礎結構重新找到憑證。

若要透過組態指定用戶端的用戶端認證值

  1. <behavior> of <endpointBehaviors> 項目加入至 <behaviors> 項目。

  2. <serviceCredentials> 項目加入至 <behaviors> 項目。請務必將必要的 name 屬性設定為適當值。

  3. <clientCertificate> of <serviceCredentials> 項目加入至 <clientCredentials> 項目。

  4. 如下列程式碼所示,將下列屬性設定為 storeLocationstoreNamex509FindTypefindValue 的適當值。如需 憑證的詳細資訊,請參閱使用憑證

    <behaviors>
       <endpointBehaviors>
          <behavior name="endpointCredential">
            <clientCredentials>
              <clientCertificate findValue="Contoso.com" 
                                 storeLocation="LocalMachine"
                                 storeName="TrustedPeople"
                                 x509FindType="FindBySubjectName" />
            </clientCredentials>
          </behavior>
       </endpointBehaviors>
    </behaviors>
    
  5. 設定用戶端時,設定 <endpoint> 項目的 behaviorConfiguration 屬性來指定行為,如下列程式碼所示。端點項目是 <client> 項目的子系。同時,將 bindingConfiguration 屬性設定為用戶端的繫結,以指定繫結組態的名稱。如果您使用的是產生的組態檔,繫結名稱就會自動產生。在這個範例中,名稱為 "tcpBindingWithCredential"

    <client>
      <endpoint name =""
                address="net.tcp://contoso.com:8036/aloha"
                binding="netTcpBinding"
                bindingConfiguration="tcpBindingWithCredential"
                behaviorConfiguration="credentialBehavior" />
    </client>
    

請參閱

工作

HOW TO:建立 Windows Communication Foundation 用戶端

參考

NetTcpBinding
SetCertificate
X509CertificateRecipientServiceCredential
ClientBase
X509CertificateInitiatorClientCredential

概念

WCF 安全性程式設計
選取認證類型
使用憑證

其他資源

ServiceModel Metadata Utility Tool (Svcutil.exe)
<netTcpBinding>
<security> of <netTcpBinding>
<message> element of <netTcpBinding>
<behavior> of <endpointBehaviors>
<behaviors>
<clientCertificate> of <serviceCredentials>
<clientCredentials>