自定义SOAP消息头

简介:

对于WebService调用,为了验证调用者的身份,可以自定义一个SoapHeader,让调用者将身份信息放在里面,然后在服务端检查,具体方法如下:

1、先定义一个SoapHeader,用它来传递身份信息:

 
  1. using System; 
  2. using System.Web.Services.Protocols; 
  3.  
  4. namespace CustomSoap 
  5.     /// <summary> 
  6.     /// 自定义SOAP头,从SoapHeader派生 
  7.     /// </summary> 
  8.     public class ServiceHeader : SoapHeader 
  9.     { 
  10.         /// <summary> 
  11.         /// 定义用户名字段 
  12.         /// </summary> 
  13.         public string Name { getset; } 
  14.         /// <summary> 
  15.         /// 定义密码字段 
  16.         /// </summary> 
  17.         public string Pass { getset; } 
  18.     } 


2、WebService中的服务方法要修改一下:

 
  1. using System; 
  2. using System.Web.Services; 
  3. using System.Web.Services.Protocols; 
  4.  
  5. namespace CustomSoap 
  6.     [WebService(Namespace = "CustomSoap.Test")] 
  7.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
  8.     [System.ComponentModel.ToolboxItem(false)] 
  9.     public class Service : System.Web.Services.WebService 
  10.     { 
  11.         /// <summary> 
  12.         /// 定义一个ServiceHeader对象 
  13.         /// </summary> 
  14.         public ServiceHeader Header { getset; } 
  15.  
  16.         /// <summary> 
  17.         /// 服务方法,用SoapHeader标记使用哪个头,此处是上面定义的Header属性 
  18.         /// </summary> 
  19.         /// <returns></returns> 
  20.         [WebMethod] 
  21.         [SoapHeader("Header")] 
  22.         public string Hello() 
  23.         { 
  24.             string user = this.Header.Name; 
  25.             string pass = this.Header.Pass; 
  26.  
  27.             //在此处可以进行身份判断,这里是写死了用户名密码 
  28.             if(string.Equals(user, "root") && string.Equals(pass, "pass")) 
  29.                 return "Hello root"
  30.             else 
  31.                 return "Login Required"
  32.         } 
  33.     } 


3、调用者要传递身份信息:

 
  1. public string CallHello() 
  2. //ServiceProxy是针对Service.asmx生成的代理类 
  3.     var proxy = new CustomSoap.Remote.ServiceProxy(); 
  4.  
  5. //传递身份信息 
  6.     proxy.ServiceHeaderValue = new CustomSoap.Remote.ServiceHeader(); 
  7.     proxy.ServiceHeaderValue.Name = "root"
  8.     proxy.ServiceHeaderValue.Pass = "pass"
  9.  
  10. //调用远程方法  
  11.     return proxy.Hello(); 

调用一下,应该能收到“Hello root”,如果用户名或密码错误,会收到“Login Required”。


此时的SOAP内容会发生变化,抓一下包或者直接在浏览器上访问Service.asmx?op=Hello,可以看到请求包:

 
  1. POST /Service.asmx HTTP/1.1 
  2. Host: localhost 
  3. Content-Type: text/xml; charset=utf-8 
  4. Content-Length: length 
  5. SOAPAction: "CustomSoap.Test/Hello" 
  6.  
  7. <?xml version="1.0" encoding="utf-8"?> 
  8. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
  9. <!--这里多出来了Header,内容就是我们自定义的ServiceHeader--> 
  10.   <soap:Header> 
  11.     <ServiceHeader xmlns="CustomSoap.Test"> 
  12.       <Name>string</Name> 
  13.       <Pass>string</Pass> 
  14.     </ServiceHeader> 
  15.   </soap:Header> 
  16. <!--END-->
  17.   <soap:Body> 
  18.     <Hello xmlns="CustomSoap.Test" /> 
  19.   </soap:Body> 
  20. </soap:Envelope> 


另外提一下,对于WebService,是明文的SOAP通讯,安全性上需要各位自己考虑一下方案。

 




     本文转自 BoyTNT 51CTO博客,原文链接:http://blog.51cto.com/boytnt/837345,如需转载请自行联系原作者


相关文章
|
安全 网络协议 数据格式
HTTP的概念以及请求消息的数据格式
HTTP的概念以及请求消息的数据格式
62 0
|
网络协议 安全 应用服务中间件
网络-http协议学习笔记(消息结构、请求方法、状态码等)
网络-http协议学习笔记(消息结构、请求方法、状态码等)
137 0
网络-http协议学习笔记(消息结构、请求方法、状态码等)
|
数据安全/隐私保护
SIP响应消息和头字段
SIP响应消息和头字段
|
XML 网络架构 数据格式
Jmeter系列(28)- 发送 soap 协议的接口
Jmeter系列(28)- 发送 soap 协议的接口
206 0
Jmeter系列(28)- 发送 soap 协议的接口
|
数据安全/隐私保护 网络架构