构建安全的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,如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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的网络服务方法
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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应用程序,如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----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>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
-----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的方法提高安全性还是有一定限制的。在安全不是特别重要的应用情形中,推荐采用此种解决方案,因为它方便快捷,灵活易用。
转自【 jillzhang 】的blog :http://www.cnblogs.com/jillzhang/archive/2007/05/22/730901.html
感谢原作者。
如何解决网络服务的安全问题,我主要从以下两个层面进行分析:
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,如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
首先,先看看没有设置SoapHeader的情况下,Soap消息为:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
点<soap:Header>正是通过这个节点,SoapMessage将信息传递给了网络服务端,网络服务端便可以从中解析出来,并加以处理,从上面的SoapMessage中,我们也看出,用户名和密码是以明文的格式传输的,这样,SoapHeader就更像Http协议中的Cookie了,我们可以参考Cookie的使用,来扩展SoapHeader,让它变得更加安全些,但总的看来,通过直接设置SoapHeader的方法提高安全性还是有一定限制的。在安全不是特别重要的应用情形中,推荐采用此种解决方案,因为它方便快捷,灵活易用。
转自【 jillzhang 】的blog :http://www.cnblogs.com/jillzhang/archive/2007/05/22/730901.html
感谢原作者。
-----------------------------------------------------------------