web Service SoapHeader 验证

=====asmx.cs=========
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

/// <summary>
/// TestService 的摘要说明
/// </summary>
[WebService(Namespace = "http://xx.comcn/web")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestService : System.Web.Services.WebService {

    public TestService ()
    {
        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    public SoapCheck soapCheck = new SoapCheck();

    [SoapHeader("soapCheck")]
    [WebMethod(Description = "TESTSOAP GetReturn")]
    public string HelloWorld(string Name) {
       if (soapCheck.CheckAccount())
        {
            //Thread.Sleep(5000);
            return Name + ",Hello World";
       }
       else
        {
            return "no";
        }
    }
   
}

//SoapHeader
public class SoapCheck : System.Web.Services.Protocols.SoapHeader
{
    public string strUser, strPassword;

    public bool CheckAccount()
    {
        if (strUser.Equals("admin") && strPassword.Equals("password"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
 


//aspx

 protected void btnService_Click( object sender ,EventArgs e)
    {//testService 引用名
        testService.SoapCheck soapcheck = new testService.SoapCheck();
        soapcheck.strUser = "admin";
        soapcheck.strPassword = "password";

        testService.TestService testService = new testService.TestService();
        testService.SoapCheckValue = soapcheck;

        Response.Write(testService.HelloWorld("嗨,你好啊"));
       
    }

你可能感兴趣的:(web Service SoapHeader 验证)