public class WebAppService : System.Web.Services.WebService
    {
        //实例化一个用于验证的user类
        public Users u = new Users();

        [WebMethod]
        [SoapHeader("u")]//接口验证需要传入u(也就是User类)作为验证信息的载体
        public string HelloWorld()
        {
            string message = "";
            if (!new ValidUsers().ValidSoapHeader(u, out message))
                return message;
            else
            return "调用成功";
        }
/*下边是对接口进行调用的方法(也就是客户端如何进行调用),我是直接在本项目中添加服务引用、并调用的*/ [WebMethod] public string TestWebService() { //实例化webservice接口 AppService.WebAppServiceSoapClient myservice = new AppService.WebAppServiceSoapClient(); //填入用于自定义验证的用户名、密码 AppService.Users user = new AppService.Users(); user.account = "admin"; user.password = "admin"; //开始调用webservice。如果有参数调用,直接在user后依次传入。如:HelloWord(user,id) string result= myservice.HelloWorld(user); return result; } } //User类 public class Users : System.Web.Services.Protocols.SoapHeader//此处必须继承该父类,标明是SoapHeader类 { public string account { get; set; } public string password { get; set; } } //验证方法 public class ValidUsers { public bool ValidSoapHeader(Users u,out string message) { if (u.account == "admin" && u.password == "admin") { message = "验证通过"; return true; } else { message = "用户名密码有误"; return false; } } }

  

posted on 2019-02-27 16:21  一禅·小和尚  阅读(619)  评论(0编辑  收藏  举报