共用方式為


憑證用戶端的訊息安全性

下列狀況顯示使用訊息安全性模式加以保障的 Windows Communication Foundation (WCF) 用戶端及服務。用戶端與服務皆以憑證驗證。如需詳細資訊,請參閱 分散式應用程式安全性

如需範例應用程式,請參閱Message Security Certificate

使用憑證的訊息安全性

特性 描述

安全性模式

訊息

互通性

僅限 WCF

驗證 (伺服器)

初始使用傳輸層安全性 (TLS) 交涉需要使用服務憑證的伺服器驗證。

驗證 (用戶端)

使用用戶端憑證及 TLS 交涉。

完整性

是,使用 TLS 通訊協定

機密性

是,使用 TLS 通訊協定

傳輸

HTTP

繫結

WSHttpBinding

服務

下列程式碼和組態要獨立執行。執行下列任一步驟:

  • 使用不含組態的程式碼建立獨立服務。
  • 使用提供的組態建立服務,但不要定義任何端點。

程式碼

下列程式碼顯示如何建立使用訊息安全性產生安全內容的服務端點。

組態

可使用以下組態來取代程式碼。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceCredentialsBehavior">
          <serviceCredentials>
            <serviceCertificate findValue="Contoso.com"
                                x509FindType="FindBySubjectName" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceCredentialsBehavior" 
               name="ServiceModel.Calculator">
        <endpoint address="https://localhost/Calculator" 
                  binding="wsHttpBinding"
                  bindingConfiguration="MessageAndCerficiateClient" 
                  name="SecuredByClientCertificate"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator">
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

用戶端

下列程式碼和組態要獨立執行。執行下列其中一項:

  • 使用此程式碼 (和用戶端程式碼) 建立獨立用戶端。
  • 建立未定義任何端點位址的用戶端,然後改用可接受組態名稱當做引數的用戶端建構函式。例如:

程式碼

下列程式碼會建立用戶端。繫結會使用訊息模式安全性,而且用戶端認證類型設為 Certificate

組態

下列組態指定使用端點行為的用戶端憑證。如需關於憑證的詳細資訊,請參閱 使用憑證。程式碼也使用 <identity> 項目指定預期伺服器識別的網域名稱系統 (DNS)。如需 識別的詳細資訊,請參閱服務身分識別和驗證

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="endpointCredentialsBehavior">
          <clientCredentials>
            <clientCertificate findValue="Cohowinery.com" 
               storeLocation="LocalMachine"
              x509FindType="FindBySubjectName" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator" >
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://machineName/Calculator" 
                behaviorConfiguration="endpointCredentialsBehavior"
                binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_ICalculator"
                contract="ICalculator"
                name="WSHttpBinding_ICalculator">
        <identity>
          <dns value="Contoso.com" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>

請參閱

概念

安全性概觀
服務身分識別和驗證
使用憑證