WebService基于SoapHeader实现安全认证

来源:互联网 发布:dz论坛seo入门教程 编辑:程序博客网 时间:2024/05/22 16:46

本文仅提供通过设置SoapHeader来控制非法用户对WebService的调用,如果是WebService建议使用WSE3.0来保护Web服务,如果使用的是Viaual Studio 2008可以使用WCF,WCF里面提供了更多的服务认证方法。以下提供一种基于SoapHeader的自定义验证方式。

 

1.首先要自定义SoapHeader,须继承System.Web.Services.Protocols.SoapHeader

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4.   
  5. /// <summary>  
  6. ///自定义的SoapHeader  
  7. /// </summary>  
  8. public class MySoapHeader : System.Web.Services.Protocols.SoapHeader  
  9. {  
  10.   
  11.     private string userName = string.Empty;  
  12.     private string passWord = string.Empty;  
  13.   
  14.     /// <summary>  
  15.     /// 构造函数  
  16.     /// </summary>  
  17.     public MySoapHeader()  
  18.     {  
  19.   
  20.     }  
  21.   
  22.     /// <summary>  
  23.     /// 构造函数  
  24.     /// </summary>  
  25.     /// <param name="userName">用户名</param>  
  26.     /// <param name="passWord">密码</param>  
  27.     public MySoapHeader(string userName, string passWord)  
  28.     {  
  29.         this.userName = userName;  
  30.         this.passWord = passWord;  
  31.     }  
  32.   
  33.     /// <summary>  
  34.     /// 获取或设置用户用户名  
  35.     /// </summary>  
  36.     public string UserName  
  37.     {  
  38.         get { return userName; }  
  39.         set { userName = value; }  
  40.   
  41.     }  
  42.   
  43.     /// <summary>  
  44.     /// 获取或设置用户密码  
  45.     /// </summary>  
  46.     public string PassWord  
  47.     {  
  48.         get { return passWord; }  
  49.         set { passWord = value; }  
  50.     }  
  51. }  



 

2.添加WebService,并编写相应代码。

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.Services;  
  5.   
  6. /// <summary>  
  7. ///WebService 的摘要说明  
  8. /// </summary>  
  9. [WebService(Namespace = "http://tempuri.org/")]  
  10. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  11. public class WebService : System.Web.Services.WebService  
  12. {  
  13.   
  14.     //声明Soap头实例  
  15.     public MySoapHeader myHeader = new MySoapHeader();  
  16.   
  17.     [System.Web.Services.Protocols.SoapHeader("myHeader")]  
  18.     [WebMethod]  
  19.     public string HelloWord()  
  20.     {  
  21.         //可以通过存储在数据库中的用户与密码来验证  
  22.         if (myHeader.UserName.Equals("houlei") & myHeader.PassWord.Equals("houlei"))  
  23.         {  
  24.             return "调用服务成功!";  
  25.         }  
  26.         else  
  27.         {  
  28.             return "对不起,您没有权限调用此服务!";  
  29.         }  
  30.     }  
  31. }  


 

3.客户端调用,分别使用不设置SoapHeader与设置SoapHeader。

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace App  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.   
  13.             localhost.WebService service = new localhost.WebService();  
  14.   
  15.             //没有设置SoapHeader的服务调用  
  16.             Console.WriteLine("没有设置SoapHeader:" + service.HelloWord());  
  17.             Console.WriteLine();  
  18.   
  19.             //将用户名与密码存入SoapHeader;  
  20.             localhost.MySoapHeader header = new localhost.MySoapHeader();  
  21.             header.UserName = "houlei";  
  22.             header.PassWord = "houlei";  
  23.             service.MySoapHeaderValue = header;  
  24.   
  25.             //设置SoapHeader的服务调用  
  26.             Console.WriteLine("设置SoapHeader:" + service.HelloWord());  
  27.             Console.Read();  
  28.         }  
  29.     }  
  30. }  

 

添加自定义SoapHeader可以成功调用WebService,否则不能调用WebService,从而实现对Web Service的非法调用。这种方法存在一定的弊端,就是在每一个WebService方法上都要进行一下验证,如果用户名与密码存储在数据库中,每调用一次WebService都要访问一次数据库进行用户名与密码的验证,对于频繁调用WebService来说,数据库压力很大。然而少量WebService调用这种方式还是一种不错的选择。