共用方式為


使用憑證驗證傳輸安全性

本文討論在使用傳輸安全性時,使用 X.509 憑證進行伺服器和客戶端驗證。 如需 X.509 憑證的詳細資訊,請參閱 X.509 公鑰憑證。 憑證必須由證書頒發機構單位發行,這通常是憑證的第三方簽發者。 在 Windows Server 網域上,Active Directory 憑證服務可用來向網域上的用戶端電腦頒發憑證。 在此案例中,服務裝載於因特網資訊服務 (IIS) 底下,其設定為安全套接字層 (SSL)。 服務會設定為 SSL (X.509) 憑證,以允許客戶端驗證伺服器的身分識別。 用戶端也會使用 X.509 憑證進行設定,以允許服務驗證用戶端的身分識別。 伺服器的憑證必須由用戶端信任,而且客戶端的憑證必須由伺服器信任。 服務與用戶端如何驗證彼此身分識別的實際機制已超出本文的範圍。 如需詳細資訊,請參閱維琪百科上的 數字簽名

此案例會實作要求/回復訊息模式,如下圖所示。

使用憑證安全傳輸

如需搭配服務使用憑證的詳細資訊,請參閱 使用憑證如何:使用 SSL 憑證設定埠。 下表描述案例的各種特性。

特徵 說明
安全性模式 運輸
互操作性 使用現有的 Web 服務客戶端和服務。
認證 (伺服器)

驗證 (用戶端)
是 (使用 SSL 憑證)

是 (使用 X.509 憑證)
資料完整性 是的
數據機密性 是的
運輸 HTTPS
捆綁 WSHttpBinding

設定服務

由於此案例中的服務裝載於 IIS 底下,因此會使用 web.config 檔案進行設定。 下列 web.config 示範如何設定 WSHttpBinding 以使用傳輸安全性和 X.509 客戶端認證。

<configuration>  
  <system.serviceModel>  
    <protocolMapping>  
      <add scheme="https" binding="wsHttpBinding" />  
    </protocolMapping>  
    <bindings>  
      <wsHttpBinding>  
        <!-- configure wsHttp binding with Transport security mode and clientCredentialType as Certificate -->  
        <binding>  
          <security mode="Transport">  
            <transport clientCredentialType="Certificate"/>
          </security>  
        </binding>  
      </wsHttpBinding>  
    </bindings>  
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->  
    <behaviors>  
      <serviceBehaviors>  
        <behavior>
           <serviceDebug includeExceptionDetailInFaults="True" />  
        </behavior>  
      </serviceBehaviors>  
    </behaviors>  
  </system.serviceModel>  
</configuration>  

設定用戶端

用戶端可以在程式代碼或 app.config 檔案中設定。 下列範例示範如何在程式碼中設定用戶端。

// Create the binding.  
var myBinding = new WSHttpBinding();  
myBinding.Security.Mode = SecurityMode.Transport;  
myBinding.Security.Transport.ClientCredentialType =  
   HttpClientCredentialType.Certificate;  
  
// Create the endpoint address. Note that the machine name
// must match the subject or DNS field of the X.509 certificate  
// used to authenticate the service.
var ea = new  
   EndpointAddress("https://localhost/CalculatorService/service.svc");  
  
// Create the client. The code for the calculator
// client is not shown here. See the sample applications  
// for examples of the calculator code.  
var cc =  
   new CalculatorClient(myBinding, ea);  
  
// The client must specify a certificate trusted by the server.  
cc.ClientCredentials.ClientCertificate.SetCertificate(  
    StoreLocation.CurrentUser,  
    StoreName.My,  
    X509FindType.FindBySubjectName,  
    "contoso.com");  
  
// Begin using the client.  
Console.WriteLine(cc.Add(100, 1111));  
//...  
cc.Close();  

或者,您可以在 App.config 檔案中設定用戶端,如下列範例所示:

<configuration>  
  <system.serviceModel>  
    <client>  
      <!-- this endpoint has an https: address -->  
      <endpoint address=" https://localhost/CalculatorService/service.svc "
                behaviorConfiguration="endpointCredentialBehavior"  
                binding="wsHttpBinding"
                bindingConfiguration="Binding1"
                contract="Microsoft.Samples.TransportSecurity.ICalculator"/>  
    </client>  
    <behaviors>  
      <endpointBehaviors>  
        <behavior name="endpointCredentialBehavior">  
          <clientCredentials>  
            <clientCertificate findValue="contoso.com"  
                               storeLocation="CurrentUser"  
                               storeName="My"  
                               x509FindType="FindBySubjectName" />  
          </clientCredentials>  
        </behavior>  
      </endpointBehaviors>  
    </behaviors>  
    <bindings>  
      <wsHttpBinding>  
        <!-- configure wsHttpbinding with Transport security mode  
                   and clientCredentialType as Certificate -->  
        <binding name="Binding1">  
          <security mode="Transport">  
            <transport clientCredentialType="Certificate"/>  
          </security>  
        </binding>  
      </wsHttpBinding>  
    </bindings>  
  </system.serviceModel>  
  
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>  

另請參閱