1.新建专案及加入Web服务 WebServiceA
2.在WebServiceA.cs中加入新的public class MySOAPHeader
3.修改WebServiceA的HelloWorld() 添加SoapHeader
4.加入Web引用 及 新建测试页面
5.测试页面的按钮事件
================================
1.新建专案及加入Web服务 WebServiceA
2.在WebServiceA.cs中加入新的public class MySOAPHeader
-----------------------------------------------------
public class MySOAPHeader : System.Web.Services.Protocols.SoapHeader
{
public string UserPWD;
public string UserName;
public bool IsValidUser()
{
string strUserName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userName", "md5");
string strUserPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userPWD", "md5");
if ( (UserName == strUserName) && (UserPWD == strUserPWD))
{
return true;
}
else
{
return false;
}
}
}
3.修改WebServiceA的HelloWorld() 添加SoapHeader
----------------------------------------------
/// <summary>
/// WebServiceA 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebServiceA : System.Web.Services.WebService {
public WebServiceA ()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
public MySOAPHeader userSOAPHeader ;
[WebMethod, SoapHeader("userSOAPHeader")]
public string HelloWorld()
{
if (userSOAPHeader.IsValidUser())
{
return "Hello World";
}
else
{
return "Sorry";
}
}
}
4.加入Web引用 及 新建测试页面
5.测试页面的按钮事件
-------------------
protected void Button1_Click(object sender, EventArgs e)
{
//
string strUserName = this.txt_UserName.Text.Trim();
strUserName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserName, "md5");
string strUserPWD = this.txt_UserPWD.Text.Trim();
strUserPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPWD, "md5");
//
localhost.WebServiceA wsA = new localhost.WebServiceA();
//
localhost.MySOAPHeader userSOAPHeader = new localhost.MySOAPHeader();
userSOAPHeader.UserName = strUserName;
userSOAPHeader.UserPWD = strUserPWD;
//
wsA.MySOAPHeaderValue = userSOAPHeader;
string strReturn = wsA.HelloWorld();
//
this.TextBox1.Text = strReturn;
}