WCF常用安全方案(五)

來源:互聯網
上載者:User

利用憑證驗證的傳輸安全

下面的方案示範由 X.509 憑證保護的 Windows Communication Foundation (WCF) 用戶端和服務。每個用戶端都有一個可用於用戶端安全通訊端層 (SSL) 身分識別驗證,並受到服務信任的認證。此樣本示範一種請求/回複訊息模式。

有關 將認證用於服務的更多資訊,請參見使用認證和如何:使用 SSL 憑證配置連接埠。


特徵 說明

安全模式

傳輸

互通性

與現有 Web 服務用戶端和服務。

身分識別驗證(伺服器)

身分識別驗證(用戶端)

是(使用 HTTPS)

是(使用認證)

完整性

保密性

Transport

HTTPS

綁定

WSHttpBinding

服務

下面的代碼和配置應單獨運行。請執行下列操作之一:

  • 使用代碼(而不使用配置)建立獨立服務。
  • 使用提供的配置建立服務,但不定義任何終結點。
代碼

下面的代碼示範如何建立使用傳輸安全和認證的服務端點。

配置

下面的配置可代替代碼用於設定服務:

<bindings>    <wsHttpBinding>        <binding name="CertificateWithTransport">            <security mode="Transport">                <transport clientCredentialType="Certificate"/>            </security>        </binding>    </wsHttpBinding></bindings><services>    <service name="ServiceModel.Calculator"              behaviorConfiguration="credentialConfig" >    <endpoint address=""              binding="wsHttpBinding"              bindingConfiguration="CertificateWithTransport"               contract="ServiceModel.ICalculator" />     </service></services><behaviors>    <serviceBehaviors>        <behavior name="credentialConfig">            <serviceCredentials>                <clientCertificate trustedStoreLocation="LocalMachine"                                    revocationMode="Online"/>            </serviceCredentials>        </behavior>    </serviceBehaviors></behaviors>

用戶端

下面的代碼和配置應獨立運行。請執行下列操作之一:

  • 使用代碼(和用戶端代碼)建立獨立用戶端。
  • 建立不定義任何端點位址的用戶端。而使用將配置名稱作為參數的用戶端建構函式。例如:
代碼

下面的代碼建立用戶端。綁定配置為使用傳輸模式安全(採用 TCP 傳輸協議),並且用戶端憑據類型設定為 Windows。

配置

下面的配置代碼是針對該用戶端,並且同樣指定了認證位置以及用於尋找它的值。

<?xml version="1.0" encoding="utf-8"?><configuration>  <system.serviceModel>    <behaviors>      <endpointBehaviors>        <behavior name="credentialConfiguration">          <clientCredentials>            <clientCertificate findValue="Contoso.com"                      storeLocation="CurrentUser"                       x509FindType="FindBySubjectName" />          </clientCredentials>        </behavior>      </endpointBehaviors>    </behaviors>    <bindings>      <wsHttpBinding>        <binding name="WSHttpBinding_ICalculator">          <security mode="Transport">            <transport clientCredentialType="Certificate" />          </security>        </binding>      </wsHttpBinding>    </bindings>    <client>      <endpoint address="https://machineName/Calculator"                behaviorConfiguration="credentialConfiguration"                binding="wsHttpBinding"                bindingConfiguration="WSHttpBinding_ICalculator"                contract="ICalculator"                name="WSHttpBinding_ICalculator" />    </client>  </system.serviceModel></configuration>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.