WebService: IssueVision 里的 SoapHeader 验证
WebService 要来就是公平来的,如果有些方法不需要被别人使用,只允许通过特定许可的人使用,便可以用到 SoapHeader 验证。在 IssueVision 项目中就有用到。现在把一些东西记下来。
首先,新建一个项目(WinForm或Asp.net),我建的叫Win5,再在解决方案中加入一个 WebService,假设名字叫 Service。
在 WebService 中进行如下操作
1.加入一个自定义的 SoapHeader类,MySoapHeader。如下:
 using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.Services;
using System.Web.Services; using System.Web.Services.Protocols;
using System.Web.Services.Protocols;

 /// <summary>
/// <summary> /// MySoapHeader 的摘要说明
/// MySoapHeader 的摘要说明 /// </summary>
/// </summary> public class MySoapHeader:SoapHeader
public class MySoapHeader:SoapHeader {
{ private string m_username;
    private string m_username; private string m_password;
    private string m_password;
 public string Username
    public string Username {
    { get
        get {
        { return m_username;
            return m_username; }
        } set
        set {
        { m_username = value;
            m_username = value; }
        } }
    }
 public string Password
    public string Password {
    { get
        get {
        { return m_password;
            return m_password; }
        } set
        set {
        { m_password = value;
            m_password = value; }
        } }
    }
 public MySoapHeader()
    public MySoapHeader() {
    { //
        // // TODO: 在此处添加构造函数逻辑
        // TODO: 在此处添加构造函数逻辑 //
        // }
    } }
}
里面其实定义了Username,Password两个属性而已。
接下来定义对SoapHeader进行验证的类,为了简便,只检查一下是不是为 admin 而已。
 using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.Services;
using System.Web.Services; using System.Web.Services.Protocols;
using System.Web.Services.Protocols;
 /// <summary>
/// <summary> /// HeaderCheck 的摘要说明
/// HeaderCheck 的摘要说明 /// </summary>
/// </summary> public class HeaderCheck
public class HeaderCheck {
{ public static void check(Service srv)
    public static void check(Service srv) {
    { if (!( srv.MyHeader.Username=="admin" && srv.MyHeader.Password=="admin"))
        if (!( srv.MyHeader.Username=="admin" && srv.MyHeader.Password=="admin")) {
        { throw new SoapException("Audit Fail", SoapException.ClientFaultCode, "Security");
            throw new SoapException("Audit Fail", SoapException.ClientFaultCode, "Security"); }
        } }
    }
 public HeaderCheck()
    public HeaderCheck() {
    { //
        // // TODO: 在此处添加构造函数逻辑
        // TODO: 在此处添加构造函数逻辑 //
        // }
    } }
}
里面的 check 是检查用的方法,Service 是 WebSerice 的类名 srv是其一个变量,这个变量要的格式在 .asmx.cs 中有定义,这个方法并没有返回任何值,他也不需要返回值,如果通过验证,就什么都不用管,如果不通过验证,也不返回假值,而是直接抛出一个异常(SoapException)。这样一来,如果通不过验证,就会出现异常,自然也就得不到结果。
再来设计WebService 的主代码页,定义一个MySoapHeader 的属性用于验证。在每个 WebMethod 上加个 SoapHeader 这个标签,这样,就能得到客户端传来的 SoapHeader,在 WebMethod 里面,要用一个check方法来检查这个 SoapHeader 合法不。每个 WebMethod 都要,麻烦。
 using System;
using System; using System.Web;
using System.Web; using System.Web.Services;
using System.Web.Services; using System.Web.Services.Protocols;
using System.Web.Services.Protocols;
 [WebService(Namespace = "http://tempuri.org/")]
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService
public class Service : System.Web.Services.WebService {
{ private MySoapHeader m_myheader;
    private MySoapHeader m_myheader;
 public MySoapHeader MyHeader
    public MySoapHeader MyHeader {
    { get
        get {
        { return m_myheader;
            return m_myheader; }
        } set
        set {
        { m_myheader = value;
            m_myheader = value; }
        } }
    }
 public Service () {
    public Service () {
 //如果使用设计的组件,请取消注释以下行
        //如果使用设计的组件,请取消注释以下行  //InitializeComponent();
        //InitializeComponent();  }
    }
 [WebMethod]
    [WebMethod] public string HelloWorld() {
    public string HelloWorld() { return "Hello World";
        return "Hello World"; }
    }
 [SoapHeader("MyHeader")]
    [SoapHeader("MyHeader")] [WebMethod(Description="Plus Two Int")]
    [WebMethod(Description="Plus Two Int")] public int Plus(int a, int b)
    public int Plus(int a, int b) {
    { HeaderCheck.check(this);
        HeaderCheck.check(this); return a + b;
        return a + b; }
    } 
     }
}
OK,WebService 告一段落,生成一下。下面是客户端,我用的是WinForms
将上面的 WebSerice 引用下来,叫做 localhost ,然后代码如下调用
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.ComponentModel;
using System.ComponentModel; using System.Data;
using System.Data; using System.Drawing;
using System.Drawing; using System.Text;
using System.Text; using System.Windows.Forms;
using System.Windows.Forms;
 namespace Win5
namespace Win5 {
{ public partial class Form1 : Form
    public partial class Form1 : Form {
    { public Form1()
        public Form1() {
        { InitializeComponent();
            InitializeComponent(); }
        }
 private void Form1_Load(object sender, EventArgs e)
        private void Form1_Load(object sender, EventArgs e) {
        { Win5.localhost.Service ws = new Win5.localhost.Service();
            Win5.localhost.Service ws = new Win5.localhost.Service(); Win5.localhost.MySoapHeader header = new Win5.localhost.MySoapHeader();
            Win5.localhost.MySoapHeader header = new Win5.localhost.MySoapHeader();  header.Username = "admin";
            header.Username = "admin"; header.Password = "adin";
            header.Password = "adin"; ws.MySoapHeaderValue = header;  //多了一个"value"
            ws.MySoapHeaderValue = header;  //多了一个"value" int r = ws.Plus(1, 2);
            int r = ws.Plus(1, 2); MessageBox.Show(r.ToString());
            MessageBox.Show(r.ToString()); }
        } }
    } }
}至此,全部完成。以后每种方法调用的时候,只要是 ws 的,都会加上 header 头,进行验证。客户端倒是省事,只要设置一次 SoapHeader 头,而服务端,将每个 WebMethod 都要验证一次 SoapHeader 头是否合法。
 
                    
                     
                    
                 
                    
                
 


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号