1.新建专案及加入Web服务WebServiceA
2.在WebServiceA.cs中加入新的publicclassMySOAPHeader
-----------------------------------------------------
publicclassMySOAPHeader:System.Web.Services.Protocols.SoapHeader
{
publicstringUserPWD;
publicstringUserName;
publicboolIsValidUser()
{
stringstrUserName=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userName","md5");
stringstrUserPWD=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userPWD","md5");
if((UserName==strUserName)&&(UserPWD==strUserPWD))
{
returntrue;
}
else
{
returnfalse;
}
}
}
3.修改WebServiceA的HelloWorld()添加SoapHeader
----------------------------------------------
///
///WebServiceA的摘要说明
///
[WebService(Namespace="http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
publicclassWebServiceA:System.Web.Services.WebService{
publicWebServiceA()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
publicMySOAPHeaderuserSOAPHeader;
[WebMethod,SoapHeader("userSOAPHeader")]
publicstringHelloWorld()
{
if(userSOAPHeader.IsValidUser())
{
return"HelloWorld";
}
else
{
return"Sorry";
}
}
}
4.加入Web引用及新建测试页面
5.测试页面的按钮事件
-------------------
protectedvoidButton1_Click(objectsender,EventArgse)
{
//
stringstrUserName=this.txt_UserName.Text.Trim();
strUserName=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserName,"md5");
stringstrUserPWD=this.txt_UserPWD.Text.Trim();
strUserPWD=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPWD,"md5");
//
localhost.WebServiceAwsA=newlocalhost.WebServiceA();
//
localhost.MySOAPHeaderuserSOAPHeader=newlocalhost.MySOAPHeader();
userSOAPHeader.UserName=strUserName;
userSOAPHeader.UserPWD=strUserPWD;
//
wsA.MySOAPHeaderValue=userSOAPHeader;
stringstrReturn=wsA.HelloWorld();
//
this.TextBox1.Text=strReturn;
} |