给访问WEB SERVICE加上一层安全性。 1.新建一个WEB SERVICE项目。 2.在项目中新建一个类MySoapHeader.cs public class MySoapHeader : SoapHeader { private string username; private string password; public string UserName { get { return username; } set { username = value; } } public string PassWord { get { return password; } set { password = value; } } public bool IsValid(string username, string pwd, out string msg) { msg = ""; if (username == "aa" && pwd == "bb") { return true; } else { msg = "对不起,用户名或密码错误"; return false; } } public bool IsValid(out string msg) { return IsValid(username, password, out msg); } } 3.修改服务内容: [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { public MySoapHeader mysh; [SoapHeader("mysh",Direction=SoapHeaderDirection.InOut)] [WebMethod(EnableSession=true,Description="自定义的HELLO方法")] public string HelloWorld(string context) { string msg; if (!mysh.IsValid(out msg)) { return msg; } return "你成功登陆:"+context; } } 4.客户端添加服务引用后,调用服务中方法。 Client.MySoapHeader soap = new Client.MySoapHeader(); soap.UserName = "aa"; soap.PassWord = "cc"; Client.Service1 server = new Client.Service1(); server.MySoapHeaderValue = soap; string msg = server.HelloWorld("我是张锐"); Response.Write("<font color=red>"+msg+"</font>");