SSL/TLS 應用於無Svc檔案的WCF

來源:互聯網
上載者:User

  在許多項目中,我們可能需要使用SSL/TLS(SSL Security Socket Layer 安全通訊端/TSL Transfer Layer Security 傳輸層安全)來對用戶端與服務端進行認證,並使用用戶端與服務端之間認證成功後產生的工作階段金鑰(Session Key)對資料加密,以保證訊息的機密性。

  應用SSL/TLS,很自然想到認證。.Net平台下,可以使用makeCert來製作測試認證。關於makeCert的使用,可以參見MSDN。

  本節內容:

    1、認證製作

    2、無Svc檔案的服務端配置

    3、用戶端調用

  1、認證製作

  本例中使用的製作認證的命令如下:

makecert -r -pe -n "CN= Tyb1222 " -b 01/01/2000 -e 01/01/2050 -eku 1.3.6.1.5.5.7.3.1   -ss my -sr localMachine -sky exchange -sp   "Microsoft RSA SChannel Cryptographic Provider" -sy 12

注意以上命令紅色部分表示認證主題。

  -ss my表示認證的儲存區為個人。

  -sr localMachine 表示儲存於本機電腦

這些資訊在稍後的配置中會用到。其他的請參見MSDN中的說明

  查看認證

 

  2、無Svc檔案的服務端配置

<?xml version="1.0" encoding="utf-8"?><!--  有關如何配置 ASP.NET 應用程式的詳細訊息,請訪問http://go.microsoft.com/fwlink/?LinkId=169433  --><configuration>  <system.serviceModel>    <bindings>      <basicHttpBinding>        <binding  name="vrvTransportBinding">                   <security mode="Transport">            <transport clientCredentialType="None"></transport>          </security>        </binding>              </basicHttpBinding>    </bindings>    <serviceHostingEnvironment>      <serviceActivations>        <add service="VrvService.StateGrid.TerminalService" relativeAddress="TerminalService.svc"/>      </serviceActivations>          </serviceHostingEnvironment>    <services>      <service name="VrvService.StateGrid.TerminalService" behaviorConfiguration="vrvServiceBehavior">        <endpoint address=""                  binding="basicHttpBinding"                   contract="VrvService.Interface.ITerminal"                   behaviorConfiguration="vrvEndpointBehavior"                   bindingConfiguration="vrvTransportBinding">                   </endpoint>        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>      </service>         </services>    <behaviors>      <serviceBehaviors>        <behavior name="vrvServiceBehavior">          <serviceMetadata httpsGetEnabled="true"/>          <serviceDebug includeExceptionDetailInFaults="True"/>          <serviceCredentials>            <windowsAuthentication allowAnonymousLogons="true"/>            <clientCertificate>              <authentication certificateValidationMode="PeerTrust"/>            </clientCertificate>            <serviceCertificate findValue="Tyb1222" storeName="My" storeLocation="LocalMachine" x509FindType="FindBySubjectName"/>          </serviceCredentials>        </behavior>      </serviceBehaviors>    </behaviors>  </system.serviceModel></configuration>

  3、用戶端調用

  可以通過添加引用調用服務:如,單擊確定即可:

 

  用戶端調用代碼如下:

            var proxy = new TerminalClient();            proxy.GetTerminalAlarm("123123", 10);

  運行代碼出現如下異常:

  由於認證是使用信任鏈結模式對進行認證的,如果認證不收信,將導致以上異常。解決辦法如下:

1、將認證匯入“受根信任憑證授權單位”,如:

在的匯入視窗中操作後,用戶端調用就不會有以上異常。

2、在C#用戶端調用時,可以通過代碼回調認證的認證方式:

                ServicePointManager.ServerCertificateValidationCallback +=                (sender, certificate, chain, sslPolicyError) => true;            var proxy = new TerminalClient();            proxy.GetTerminalAlarm("123123", 10);    

  參考:

http://www.cnblogs.com/artech/archive/2011/06/02/Authentication_042.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.