作法:指定用戶端認證值

使用 Windows Communication Foundation (WCF) 時,此服務可以指定用戶端向服務驗證的方式。 例如,服務可以規定用戶端必須出示憑證交付驗證。

判斷用戶端認證類型

  1. 從服務的中繼資料端點擷取中繼資料。 中繼資料通常包含兩個檔案:以您所選程式語言 (預設為 Visual C#) 撰寫的用戶端程式碼,還有 XML 組態檔。 擷取中繼資料的方法之一,是使用 Svcutil.exe 工具傳回用戶端程式碼和用戶端組態。 如需詳細資訊,請參閱擷取中繼資料ServiceModel 中繼資料公用程式工具 (Svcutil.exe)

  2. 開啟 XML 組態檔。 如果您是使用 Svcutil.exe 工具,檔案的預設名稱即為 Output.config。

  3. 尋找具有 mode 屬性的 <security> 元素 (<security mode =MessageOrTransport>,其中 MessageOrTransport 會設定為其中一種安全性模式)。

  4. 找出符合模式值的子項目。 例如,如果模式設定為 Message,則尋找包含在 <security> 元素中的 <message> 元素。

  5. 記下已指派給 clientCredentialType 屬性的值。 實際值取決於使用的模式是傳輸還是訊息。

下列 XML 程式碼所示為使用訊息安全性,且用戶端驗證時需要憑證的用戶端組態。

<security mode="Message">
    <transport clientCredentialType="Windows" proxyCredentialType="None"
        realm="" />
     <message clientCredentialType="Certificate" negotiateServiceCredential="true"
    algorithmSuite="Default" establishSecurityContext="true" />
</security>

範例:TCP 傳輸模式結合用戶端認證的憑證

這個範例會將安全性模式設為傳輸模式,並將用戶端認證值設為 X.509 憑證。 下列程序示範如何透過程式碼與組態,設定用戶端的用戶端認證值。 這會假設您已使用 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 從服務傳回中繼資料 (程式碼和組態)。 如需詳細資訊,請參閱操作說明:建立用戶端

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

  1. 使用 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 從服務產生程式碼和組態。

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

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

    // Create a binding using Transport and a certificate.
    NetTcpBinding b = new NetTcpBinding();
    b.Security.Mode = SecurityMode.Transport;
    b.Security.Transport.ClientCredentialType =
        TcpClientCredentialType.Certificate;
    
    // Create an EndPointAddress.
    EndpointAddress ea = new EndpointAddress(
        "net.tcp://localHost:8036/Calculator/MyCalculator");
    
    // Create the client.
    CalculatorClient cc = new CalculatorClient(b, ea);
    
    // Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate(
        StoreLocation.LocalMachine,
        StoreName.My,
        X509FindType.FindBySubjectName,
        "cohowinery.com");
    try
    {
        cc.Open();
        // Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2));
        cc.Close();
    }
    catch (AddressAccessDeniedException adExc)
    {
        Console.WriteLine(adExc.Message);
        Console.ReadLine();
    }
    catch (System.Exception exc)
    {
        Console.WriteLine(exc.Message);
        Console.ReadLine();
    }
    
    ' Create a binding using Transport and a certificate.
    Dim b As New NetTcpBinding()
    b.Security.Mode = SecurityMode.Transport
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
    
    ' Create an EndPointAddress.
    Dim ea As New EndpointAddress("net.tcp://localHost:8036/Calculator/MyCalculator")
    
    ' Create the client.
    Dim cc As New CalculatorClient(b, ea)
    
    ' Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate( _
    StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "cohowinery.com")
    Try
        cc.Open()
        ' Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2))
        cc.Close()
    Catch adExc As AddressAccessDeniedException
        Console.WriteLine(adExc.Message)
        Console.ReadLine()
    Catch exc As System.Exception
        Console.WriteLine(exc.Message)
        Console.ReadLine()
    End Try
    

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

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

  1. <behavior> 元素新增至 <behaviors> 元素。

  2. <clientCredentials> 元素新增至 <behaviors> 元素。 請務必將必要的 name 屬性設定為適當值。

  3. <clientCertificate> 元素新增至 <clientCredentials> 元素。

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

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

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

另請參閱