天天看點

WCF BasicHttpBinding 安全解析(3)預設安全設定(IIS宿主)

本節開始的執行個體采用IIS作為WCF宿主,使用的契約和實作和前面使用的仍然相同,下面我們建構兩個站點,一個WCF服務宿主站點,一個服務測試站點。首先我們建立服務端,打開vs2010à檔案à建立項目à選擇WCF模闆àWCF服務應用程式,如圖11-31。

<a href="http://images.cnblogs.com/cnblogs_com/xuanhun/201106/201106281015429742.gif"></a>

圖11-31 建立WCF如無應用程式

删除預設添加的SVC檔案和接口檔案,添加接口檔案IHelloService.cs和接口實作檔案HelloService.cs,代碼内容和前面章節使用的相同。現在按代碼清單11-78的方式配置web.config檔案。

代碼清單11-78 服務端web.config

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;configuration&gt;

  &lt;system.serviceModel&gt;

    &lt;serviceHostingEnvironment&gt;

    &lt;serviceActivations &gt;

    &lt;add relativeAddress="HelloService.svc"

service="WcfHelloService.HelloService"/&gt;

    &lt;/serviceActivations &gt;

    &lt;/serviceHostingEnvironment &gt;

    &lt;services&gt;

      &lt;service name="WcfHelloService.HelloService" behaviorConfiguration="WcfHelloService.ServiceBehavior"&gt;

        &lt;endpoint binding="basicHttpBinding" contract="WcfHelloService.IHelloService"&gt;

           &lt;/endpoint&gt;

      &lt;/service&gt;

    &lt;/services&gt;

    &lt;behaviors&gt;

      &lt;serviceBehaviors&gt;

        &lt;behavior name="WcfHelloService.ServiceBehavior"&gt;

          &lt;serviceMetadata httpGetEnabled="true" /&gt;

          &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;

        &lt;/behavior&gt;

      &lt;/serviceBehaviors&gt;

    &lt;/behaviors&gt;

  &lt;/system.serviceModel&gt;

&lt;/configuration&gt;

現在我對代碼清單11-78中的關鍵配置做簡要的說明。清單中的&lt;serviceActivations&gt;配置節用于添加可定義虛拟服務激活設定(映射到WCF) 服務類型)的設定。使用此配置元素可以在不使用.svc檔案的情況下激活承載在WAS/IIS中的服務。上面的配置中我們将WcfHelloService.HelloService服務的請求映射到虛拟的HelloService.svc。配置&lt;serviceMetadata httpGetEnabled="true" /&gt;,使得我們可以通過httpGet的方式通路中繼資料。配置檔案配置妥當之後我們打開IIS添加名為wcfservicewebsite.com的站點,目錄指向項目所在的本地路徑,應用程式池采用.net 4.0,如圖11-32。

<a href="http://images.cnblogs.com/cnblogs_com/xuanhun/201106/201106281015455089.gif"></a>

圖11-32 配置服務站點

<a href="http://images.cnblogs.com/cnblogs_com/xuanhun/201106/201106281015525749.gif"></a>

圖11-33 從浏覽器通路HelloService.svc

圖11-33的結果說明服務釋出成功,接下來我們建立測試站點,使用vs2010建立一個Asp.net MVC2 Web應用程式,然後添加服務引用,輸入服務位址,最後點确定,如圖11-34。

<a href="http://images.cnblogs.com/cnblogs_com/xuanhun/201106/201106281015585363.gif"></a>

圖11-34 添加服務引用

添加引用之後,用戶端會自動在配置檔案中添加如代碼清單11-79所示的配置。

代碼清單11-79 用戶端web.config配置

  &lt;system.serviceModel&gt;

    &lt;bindings&gt;

      &lt;basicHttpBinding&gt;

        &lt;binding name="BasicHttpBinding_IHelloService" closeTimeout="00:01:00"

openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"

allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"

maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"

useDefaultWebProxy="true"&gt;

          &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;

          &lt;security mode="None"&gt;

            &lt;transport clientCredentialType="None" proxyCredentialType="None"

realm="" /&gt;

            &lt;message clientCredentialType="UserName" algorithmSuite="Default" /&gt;

          &lt;/security&gt;

        &lt;/binding&gt;

      &lt;/basicHttpBinding&gt;

    &lt;/bindings&gt;

    &lt;client&gt;

      &lt;endpoint address="http://wcfservicewebsite.com/HelloService.svc"

binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHelloService"

contract="HelloServiceReferenceForBasic.IHelloService" name="BasicHttpBinding_IHelloService" /&gt;

    &lt;/client&gt;

代碼清單11-79的配置無需做過多的解釋,但是由VS生成的配置檔案中我們能看到更多的 預設細節,比如預設的編碼方式、憑據類型、安全模式等。

修改HomeController為代碼清單11-80所示的内容。

代碼清單11-80 調用服務

[HandleError]

public class HomeController : Controller

    {

        HelloServiceReferenceForBasic.HelloServiceClient client = new HelloServiceReferenceForBasic.HelloServiceClient();

public ActionResult Index()

        {

string helloString = client.GetHello();

            ViewData["Message"] = helloString;

return View();

        }

public ActionResult About()

    }

在代碼清單11-80中,首先聲明了變量client,它是用戶端代理執行個體。然後在Index方法中調用client.GetHello()方法,最後将傳回的資料指派給ViewData["Message"],傳回的前端。在前端頁面,我們通過如代碼清單11-81的方式綁定資料。

代碼清單11-81 在前端顯示請求的到的資料。

&lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt;

&lt;h2&gt;BasicHttpBinding傳回資料:&lt;br /&gt;

&lt;%: ViewData["Message"] %&gt;&lt;/h2&gt;

&lt;/asp:Content&gt;

圖11-35 用戶端顯示取得的資料

從圖11-35的結果結合服務端的代碼,服務端沒有擷取安全上下文才傳回次結果。下面我們再看Fiddler監聽到的資料,請求資料如代碼清單11-82所示,響應資料如代碼清單11-83所示。

代碼清單11-82 用戶端請求資料

POST http://wcfservicewebsite.com/HelloService.svc HTTP/1.1

Content-Type: text/xml; charset=utf-8

VsDebuggerCausalityData: uIDPo5jCIjrL22NAjy8DHniBadAAAAAAlMqRQj7B9ka4Fz7jm+jNSHCjeEjI+TVCsG2H2EAUzR8ACQAA

SOAPAction: "http://tempuri.org/IHelloService/GetHello"

Host: wcfservicewebsite.com

Content-Length: 133

Expect: 100-continue

Accept-Encoding: gzip, deflate

Connection: Keep-Alive

&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;&lt;s:Body&gt;&lt;GetHello xmlns="http://tempuri.org/"/&gt;&lt;/s:Body&gt;&lt;/s:Envelope&gt;

代碼清單11-83 服務端響應資料

HTTP/1.1 200 OK

Vary: Accept-Encoding

Server: Microsoft-IIS/7.5

X-Powered-By: ASP.NET

Date: Sat, 25 Jun 2011 08:49:31 GMT

Content-Length: 197

&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;

&lt;s:Body&gt;

&lt;GetHelloResponse xmlns="http://tempuri.org/"&gt;

&lt;GetHelloResult&gt;hello&lt;/GetHelloResult&gt;

&lt;/GetHelloResponse&gt;

&lt;/s:Body&gt;

&lt;/s:Envelope&gt;

從代碼清單11-82和代碼清單11-83的資料可以看出預設情況下沒有對消息進行加密,也沒有任何憑據和認證資訊。當然BasicHttpBinding不是一點安全性都沒有的,下面的幾節我們逐漸的武裝它。

本文轉自懸魂部落格園部落格,原文連結:http://www.cnblogs.com/xuanhun/archive/2011/06/28/2091955.html,如需轉載請自行聯系原作者

繼續閱讀