• 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/
    原文链接:http://www.cnblogs.com/xuanhun/ 更多内容,请访问我的个人站点 对编程,安全感兴趣的,加qq群:hacking-1群:303242737,hacking-2群:147098303,nw.js,electron交流群 313717550。
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    关注我:关注玄魂的微信公众号

  • 相关阅读:
    【20220511】起风了
    【力扣 083】154. 寻找旋转排序数组中的最小值 II
    【力扣 082】153. 寻找旋转排序数组中的最小值
    【力扣 084】240. 搜索二维矩阵 II
    【力扣 081】658. 找到 K 个最接近的元素
    xshell和xftp下载免费版本方法
    端到端的特征转换示例:使用三元组损失和 CNN 进行特征提取和转换
    GAN能进行股票预测吗?
    LSTM 又回来了! 新论文使用LSTM挑战长序列建模的 ViT
    3 个不常见但非常实用的Pandas 使用技巧
  • 原文地址:https://www.cnblogs.com/xuanhun/p/2091955.html
Copyright © 2020-2023  润新知