WCF BasicHttpBinding 安全解析(3)默认安全设置(IIS宿主)

简介:


本节开始的实例采用IIS作为WCF宿主,使用的契约和实现和前面使用的仍然相同,下面我们构建两个站点,一个WCF服务宿主站点,一个服务测试站点。首先我们创建服务端,打开vs2010à文件à新建项目à选择WCF模板àWCF服务应用程序,如图11-31。

clip_image002

图11-31 创建WCF如无应用程序

删除默认添加的SVC文件和接口文件,添加接口文件IHelloService.cs和接口实现文件HelloService.cs,代码内容和前面章节使用的相同。现在按代码清单11-78的方式配置web.config文件。

代码清单11-78 服务端web.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>

  <system.serviceModel>

    <serviceHostingEnvironment>

    <serviceActivations >

    <add relativeAddress="HelloService.svc"

service="WcfHelloService.HelloService"/>

    </serviceActivations >

    </serviceHostingEnvironment >

    <services>

      <service name="WcfHelloService.HelloService" behaviorConfiguration="WcfHelloService.ServiceBehavior">

        <endpoint binding="basicHttpBinding" contract="WcfHelloService.IHelloService">

           </endpoint>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="WcfHelloService.ServiceBehavior">

          <serviceMetadata httpGetEnabled="true" />

          <serviceDebug includeExceptionDetailInFaults="false" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

现在我对代码清单11-78中的关键配置做简要的说明。清单中的<serviceActivations>配置节用于添加可定义虚拟服务激活设置(映射到WCF) 服务类型)的设置。使用此配置元素可以在不使用.svc文件的情况下激活承载在WAS/IIS中的服务。上面的配置中我们将WcfHelloService.HelloService服务的请求映射到虚拟的HelloService.svc。配置<serviceMetadata httpGetEnabled="true" />,使得我们可以通过httpGet的方式访问元数据。配置文件配置妥当之后我们打开IIS添加名为wcfservicewebsite.com的站点,目录指向项目所在的本地路径,应用程序池采用.net 4.0,如图11-32。

clip_image004

图11-32 配置服务站点

在浏览器内输入http://wcfservicewebsite.com/HelloService.svc来验证服务是否托管成功,结果如图11-33。

clip_image006

图11-33 从浏览器访问HelloService.svc

图11-33的结果说明服务发布成功,接下来我们创建测试站点,使用vs2010创建一个Asp.net MVC2 Web应用程序,然后添加服务引用,输入服务地址,最后点确定,如图11-34。

clip_image008

图11-34 添加服务引用

添加引用之后,客户端会自动在配置文件中添加如代码清单11-79所示的配置。

代码清单11-79 客户端web.config配置

  <system.serviceModel>

    <bindings>

      <basicHttpBinding>

        <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">

          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          <security mode="None">

            <transport clientCredentialType="None" proxyCredentialType="None"

realm="" />

            <message clientCredentialType="UserName" algorithmSuite="Default" />

          </security>

        </binding>

      </basicHttpBinding>

    </bindings>

    <client>

      <endpoint address="http://wcfservicewebsite.com/HelloService.svc"

binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHelloService"

contract="HelloServiceReferenceForBasic.IHelloService" name="BasicHttpBinding_IHelloService" />

    </client>

  </system.serviceModel>

代码清单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()

        {

return View();

        }

    }

在代码清单11-80中,首先声明了变量client,它是客户端代理实例。然后在Index方法中调用client.GetHello()方法,最后将返回的数据赋值给ViewData["Message"],返回的前端。在前端页面,我们通过如代码清单11-81的方式绑定数据。

代码清单11-81 在前端显示请求的到的数据。

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>BasicHttpBinding返回数据:<br />

<%: ViewData["Message"] %></h2>

</asp:Content>

下面我们在vs2010中将测试站点设为启动项,开启Http监听工具Fiddler,启动测试站点。结果如图11-35所示。clip_image010

图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

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetHello xmlns="http://tempuri.org/"/></s:Body></s:Envelope>

代码清单11-83 服务端响应数据

HTTP/1.1 200 OK

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

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

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

<s:Body>

<GetHelloResponse xmlns="http://tempuri.org/">

<GetHelloResult>hello</GetHelloResult>

</GetHelloResponse>

</s:Body>

</s:Envelope>

从代码清单11-82和代码清单11-83的数据可以看出默认情况下没有对消息进行加密,也没有任何凭据和认证信息。当然BasicHttpBinding不是一点安全性都没有的,下面的几节我们逐步的武装它。

本文转自悬魂博客园博客,原文链接:http://www.cnblogs.com/xuanhun/archive/2011/06/28/2091955.html,如需转载请自行联系原作者
相关文章
|
开发框架 安全 .NET
文件上传绕过】——解析漏洞_IIS6.0解析漏洞
文件上传绕过】——解析漏洞_IIS6.0解析漏洞
534 4
|
开发框架 安全 应用服务中间件
【文件上传绕过】——解析漏洞_IIS7.0 | IIS7.5 | Nginx的解析漏洞
【文件上传绕过】——解析漏洞_IIS7.0 | IIS7.5 | Nginx的解析漏洞
733 9
|
开发框架 安全 .NET
38、中间件漏洞解析-IIS6.0
38、中间件漏洞解析-IIS6.0
173 45
38、中间件漏洞解析-IIS6.0
|
开发框架 安全 中间件
38、中间件漏洞解析-IIS6.0
38、中间件漏洞解析-IIS6.0
142 0
|
开发框架 安全 .NET
从零到IIS建站再到IIS解析漏洞复现
从零到IIS建站再到IIS解析漏洞复现
280 0
|
4月前
|
网络安全 Windows
Windows IIS 10如何配置自签名SSL并实现自动跳转
本文记录了IIS配置自签名证书及HTTPS跳转的注意事项。包括解决443端口占用问题、URL Rewrite插件安装与配置、web.config修改方法,以及避免因旧教程导致的配置错误。
Windows IIS 10如何配置自签名SSL并实现自动跳转
|
网络协议
windows_server2012搭建iis并配置http重定向 iis转发
windows_server2012搭建iis并配置http重定向 iis转发
743 1
【Azure云服务 Cloud Service】如何在部署云服务Cloud Service时候通过启动任务Start Task来配置IIS (如开启ARR)
【Azure云服务 Cloud Service】如何在部署云服务Cloud Service时候通过启动任务Start Task来配置IIS (如开启ARR)
222 2
|
开发框架 .NET 中间件
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
212 0
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
|
Windows
iis配置http重定向302转发get请求并去掉最后的斜杠/ iis重定向 iis去除url最后的斜杠 iis重定向链接斜杠(已解决)
iis配置http重定向302转发get请求并去掉最后的斜杠/ iis重定向 iis去除url最后的斜杠 iis重定向链接斜杠(已解决)
831 0