WCF常用安全方案(四)

來源:互聯網
上載者:User

通過基本驗證確保的傳輸安全

示範一個 Windows Communication Foundation (WCF) 服務和用戶端。伺服器需要一個有效可用於安全通訊端層 (SSL) 的 X.509 憑證,並且用戶端必須信任此伺服器憑證。此外,Web 服務已經有一個可以使用的 SSL 實現。有關 啟用 Internet 資訊服務 (IIS) 中的基本驗證的更多資訊,請參見 http://go.microsoft.com/fwlink/?LinkId=83822(可能為英文網頁)。


特徵 說明

安全模式

傳輸

互通性

與現有的 Web 服務用戶端和服務進行互操作

身分識別驗證(伺服器)

身分識別驗證(用戶端)

是(使用 HTTPS)

是(通過使用者名稱/密碼)

完整性

保密性

Transport

HTTPS

綁定

WSHttpBinding

服務

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

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

下面的代碼示範如何建立使用 Windows 域使用者名稱和密碼確保傳輸安全的服務端點。請注意,此服務需求使用 X.509 憑證向用戶端進行身分識別驗證。有關更多資訊,請參見使用認證和如何:使用 SSL 憑證配置連接埠。

配置

下面將配置一個服務以使用具有傳輸級安全的基本驗證:

<?xml version="1.0" encoding="utf-8"?><configuration>    <system.serviceModel>        <bindings>            <wsHttpBinding>                <binding name="UsernameWithTransport">                    <security mode="Transport">                        <transport clientCredentialType="Basic" />                    </security>                </binding>            </wsHttpBinding>        </bindings>        <services>            <service name="BasicAuthentication.Calculator">                <endpoint address="https://localhost/Calculator"                          binding="wsHttpBinding"                           bindingConfiguration="UsernameWithTransport"                          name="BasicEndpoint"                           contract="BasicAuthentication.ICalculator" />            </service>        </services>    </system.serviceModel></configuration>

用戶端代碼

下面的代碼示範包括使用者名稱和密碼在內的用戶端代碼。請注意,此使用者必須提供一個有效 Windows 使用者名稱和密碼。此處不顯示用於返回使用者名稱和密碼的代碼。使用對話方塊或其他介面來查詢使用者的相關資訊。

注意:
使用者名稱和密碼只能使用代碼進行設定。

配置

下面的代碼示範用戶端配置。

注意:
不能使用配置來設定使用者名稱和密碼。此處顯示的配置必須使用代碼進行擴充以設定使用者名稱和密碼。

<?xml version="1.0" encoding="utf-8"?><configuration>  <system.serviceModel>    <bindings>      <wsHttpBinding>        <binding name="WSHttpBinding_ICalculator" >          <security mode="Transport">            <transport clientCredentialType="Basic" />          </security>        </binding>      </wsHttpBinding>    </bindings>    <client>      <endpoint address="https://machineName/Calculator"                 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.