使用SoapHeader对WebService进行身份验证
1.新建CheckUserSoapHeader
View Code
public class CheckUserSoapHeader : SoapHeader { public string UserName { get; set; } public string Password { get; set; } public CheckUserSoapHeader() { } public CheckUserSoapHeader(string userName,string password) { UserName = userName; Password = password; } public bool CheckLogin() { return UserName == "Admin" && Password == "888888"; } }
2.添加webServices引用
public class TestWebService : System.Web.Services.WebService { public CheckUserSoapHeader CheckUser; [SoapHeader("CheckUser")] [WebMethod(Description="校验用户名和密码,返回登录信息",EnableSession=true)] public string GetLoginMessage() { if (CheckUser.CheckLogin()) { return "登录成功!"; } return "登录失败!"; } }
3.前台调用
protected void btnLogin_Click(object sender, EventArgs e) { CheckUserWebservice.CheckUserSoapHeader header = new CheckUserWebservice.CheckUserSoapHeader(); header.UserName = txtName.Text; header.Password = txtPassword.Text; CheckUserWebservice.TestWebServiceSoapClient client = new CheckUserWebservice.TestWebServiceSoapClient(); lblMessage.Text = client.GetLoginMessage(header); }
4.整体效果