添加个Key类继承SoapHeader
public class KEY : SoapHeader
{
public KEY()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public string UserName;
public string PassWord;
public bool ValideUser(string in_UserName, string in_PassWord)
{
if ((in_UserName == "Admin") && (in_PassWord == "123456"))
{
return true;
}
else
{
return false;
}
}
}
webservice方法中添加[SoapHeader("key")]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class AccountService : System.Web.Services.WebService
{
public KEY key = new KEY();
public AccountService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
[SoapHeader("key")]
public string HelloWorld()
{
if (key.ValideUser(key.UserName, key.PassWord))
return "Hello World";
else
return "";
}
[WebMethod]
public string HiWorld()
{
return "Hi World";
}
}
调用
public string str = "";
protected void Page_Load(object sender, EventArgs e)
{
TestService.AccountServiceSoapClient ts = new TestService.AccountServiceSoapClient();
TestService.KEY k = new TestService.KEY();
k.UserName = "Admin";
k.PassWord = "123456";
str = ts.HelloWorld(k)+ts.HiWorld();
}