原文:构建安全的Xml Web Service系列之初探使用Soap头

Xml Web Service 从诞生那天就说自己都么都么好,还津津乐道的说internet也会因此而进入一个新纪元,可5年多来,Xml Web Service并没有像当初宣扬的那样火起来,尽管在一些领域之内,也有人牛刀小试,但从整体而言,Service还并没有得到广泛的应用,原因有很多,有一些来源于目前各大厂商都坚持自己的service标准,不能形成统一,也有对现有的稳定系统不愿进行更改的原因,但还包括web service本身的原因,最明显的应该是两个:1) 安全,2)性能。毕业设计的时候,写的是高性能web service的开发和应用,下面,我想用几篇文章来阐述一下有关xml web service安全的几个解决方案。欢迎各位大虾来砸。
    如何解决网络服务的安全问题,我主要从以下两个层面进行分析:
   1) 确保调用者的合法身份-保证来源的合法
   2) 在传输中不被非法监听和篡改。
当然还会有其他方面的安全隐患,希望大家能多多提出,我也好能进一步总结。
   如果您想更快的掌握本文提到的技术,您以前必须了解xml web service的工作原理,并且亲自开发并部署或者使用过Xml web service,只是您并不相信您部署的xml web service是安全的。
 本节先介绍一种最为简单的确保调用者合法的解决方案-将用户名和密码附加在Soap消息头部,在服务器端进行用户名密码验证。这种方式从解决了原网络服务不能针对特定对象产生响应的问题。但因为仍以明文格式
传输,所以不能有效地防止信息在传输过程中被偷窥,篡改或伪造。
  如果您以前已经使用了这种方法,请略过此篇文章,我下篇文章中将讲述其他方式,更加合理的解决方案,欢迎您继续关注。
   下面是实现此种解决方案的步骤,请您一步一步来
  第一步:首先您需要创建一个Xml Web Service的服务项目,创建方法如下
     打开visual studio 2005,在起始页上点击创建项目,选择visual C#中的Asp.Net web 服务应用程序,输入项目名称
  第二步:在该项目中创建一个扩展的SoapHeader对象MySoapHeader,如下

MySoapHeader
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services.Protocols;

namespace WebService1
{
    public class MySoapHeader:SoapHeader
    {
        private string _userName;
        private string _pwd;
        /**//// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }
        /**//// <summary>
        /// 密码
        /// </summary>
        public string Pwd
        {
            get
            {
                return _pwd;
            }
            set
            {
                _pwd = value;
            }
        }
    }
}

第三步:创建一个Xml Web Service,另添加一个要求使用SoapHeader的网络服务方法

WebService
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace WebService1
{
    /**//// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        public MySoapHeader header = new MySoapHeader();        
        [WebMethod]
        [SoapHeader("header")]       
        public string HelloWorld()
        {
            if (header == null)
            {
                return "您没有设置SoapHeader,不能正常访问此服务!";
            }
            if (header.UserName != "jillzhang" || header.Pwd != "123456")
            {
                return "您提供的身份验证信息有误,不能正常访问此服务!";
            }
            return "Hello World";
        }
    }
}

第四步:创建一个调用Xml Web Service的Console应用程序,如下:

TestConsole
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
       
        static void Main(string[] args)
        {
            localhost.Service1 ws = new ConsoleApplication1.localhost.Service1();
            //ws.MySoapHeaderValue = new ConsoleApplication1.localhost.MySoapHeader();
            //ws.MySoapHeaderValue.UserName = "jillzhang";
            //ws.MySoapHeaderValue.Pwd = "123456";
            Console.WriteLine(ws.HelloWorld());
        }
    }
}

下面的分析,对于大家来说,应该是最重要的,很多人不清楚SoapHeader的工作原理,为什么这么怪异的写法竟能产生神奇的效果,下面我将不同情形下的Soap消息解析出来,大家仔细观察这个信息,并可以清晰地掌握了SoapHeader的工作原理了.
首先,先看看没有设置SoapHeader的情况下,Soap消息为:

-----Soap请求 在 2007年05月22日 12时39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>

-----Soap响应 在 2007年05月22日 12时39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>您提供的身份验证信息有误,不能正常访问此服务!</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>

再看看在设置了SoapHeader之后的Soap的请求和响应信息

-----Soap请求 在 2007年05月22日 12时42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><MySoapHeader xmlns="http://tempuri.org/"><UserName>jillzhang</UserName><Pwd>123456</Pwd></MySoapHeader></soap:Header><soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>

-----Soap响应 在 2007年05月22日 12时42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>Hello World</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>

大家可以比较前后两个Soap消息的异同,会发现,加了SoapHeader的请求SoapMessage比没有加的多了一个节
点<soap:Header>正是通过这个节点,SoapMessage将信息传递给了网络服务端,网络服务端便可以从中解析出来,并加以处理,从上面的SoapMessage中,我们也看出,用户名和密码是以明文的格式传输的,这样,SoapHeader就更像Http协议中的Cookie了,我们可以参考Cookie的使用,来扩展SoapHeader,让它变得更加安全些,但总的看来,通过直接设置SoapHeader的方法提高安全性还是有一定限制的。在安全不是特别重要的应用情形中,推荐采用此种解决方案,因为它方便快捷,灵活易用。
有关Cookie的信息,请参考前期文章:Cookie-天使还是恶魔?
下一节,我将介绍一下,如何获取SoapMessage.

构建安全的Xml Web Service系列之初探使用Soap头的更多相关文章

  1. 构建安全的Xml Web Service系列之wse之证书存储位置

    原文:构建安全的Xml Web Service系列之wse之证书存储位置 我们在前几天对xml web service的安全性提出了一些建议,大家可以通过以下地址访问: 构建安全的Xml Web Se ...

  2. 构建安全的Xml Web Service系列之wse之错误代码详解

    原文:构建安全的Xml Web Service系列之wse之错误代码详解 WSE3.0现在还没有中文版的可以下载,使用英文版的过程中,难免会遇到各种各样的错误,而面对一堆毫无头绪的错误异常,常常会感到 ...

  3. 构建安全的Xml Web Service系列之SSL篇

    原文:构建安全的Xml Web Service系列之SSL篇 首先介绍一下SSL, SSL 的英文全称是 "Secure Sockets Layer" ,中文名为 "安全 ...

  4. 构建安全的Xml Web Service系列之如何察看SoapMessage

    原文:构建安全的Xml Web Service系列之如何察看SoapMessage 上一篇文章地址:构建安全的Xml Web Service系列一之初探使用Soap头 (5-22 12:53)     ...

  5. C# 开发XML Web Service与Java开发WebService

    一.web service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量 ...

  6. 深入学习Web Service系列----异步开发模式

    概述 在本篇随笔中,通过一些简单的示例来说一下Web Service中的异步调用模式.调用Web Service方法有两种方式,同步调用和异步调用.同步调用是程序继续执行前等候调用的完成,而异步调用在 ...

  7. Web Service进阶(七)浅谈SOAP Webservice和RESTful Webservice

    浅谈SOAP Webservice和RESTful Webservice REST是一种架构风格,其核心是面向资源,REST专门针对网络应用设计和开发方式,以降低开发的复杂性,提高系统的可伸缩性.RE ...

  8. XML Web Service架构图

  9. Web Service简介 内部资料 请勿转载 谢谢合作

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

随机推荐

  1. OC 获取城市首字母

    解析依据文件中面的内容,读入一个城市,输出所在首字母 比方读入 长春 输出 c 读入 北京 输出 b 不知道文本中的字体是什么格式 读取文件时 程序不能正确执行 运用oc中的字典 initWithOb ...

  2. Unity3D ITween!

    percentage +=0.001f; iTween.PutOnPath(gameObject,path,percentage); //You can cause the object to ori ...

  3. 基于Hadoop的地震数据分析统计

    源码下载地址:http://download.csdn.net/detail/huhui_bj/5645641 opencsv下载地址:http://download.csdn.net/detail/ ...

  4. .Net Core配置文件

    .Net Core下如何管理配置文件 一.前言 根据该issues来看,System.Configuration在.net core中已经不存在了,那么取而代之的是由Microsoft.Extensi ...

  5. Perl中检测标准输入使用的字符集

    #Perl中检测标准输入使用的字符集 sub locale_encode{ my $lang = $ENV{'LANG'}; my $dot_pos = rindex($lang, '.'); my ...

  6. auto_ptr and scoped_ptr

    #include "boost/scoped_ptr.hpp" #include <iostream> #include <memory>//contain ...

  7. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  8. HashMap-死锁导致cpu占用100%分析(转)

    最近项目里面的一段千年代码出了问题,这个问题以前也出现过,不过不是那么明显,这次迁移机器由以前的4台机子变成2台以后问题被放大,最终不得不解决,特此分析一下. 先放出问题的代码 ? 1 2 3 4 5 ...

  9. Ubuntu12.04下载Repo

    操作系统:Ubuntu12.04LTS 64bit "#"号后面表示凝视内容 $cd ~ #进入下载文件夹 $mkdir bin #创建bin文件夹用于存储Repo脚本 $PATH ...

  10. docker 的安装

    官方站点上有各种环境下的 安装指南,这里主要介绍下Ubuntu和CentOS系列的安装. Ubuntu 系列安装 Docker 通过系统自带包安装 Ubuntu 14.04 版本号系统中已经自带了 D ...