• Webservice加上SoapHeader验证方式


    提供一种基于SoapHeader的自定义验证方式,代码如下:

    public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
        {
            private string userID = string.Empty;
            private string userPW = string.Empty;
    
            public string UserId
            {
                get { return userID; }
                set { userID = value; }
            }
            public string UserPW
            {
                get { return userPW; }
                set { userPW = value; }
            }
            public MySoapHeader()
            { }
            public MySoapHeader(string name, string password)
            {
                userID = name;
                userPW = password;
            }
    
            private bool IsValid(string nUserId, string nPassWord, out string nMsg)
            {
                nMsg = "";
                try
                {
                    if (nUserId == "admin" && nPassWord == "admin")
                    {
                        return true;
                    }
                    else
                    {
                        nMsg = "对不起,你无权调用Web服务";
                        return false;
                    }
                }
                catch
                {
                    nMsg = "对不起,你无权调用Web服务";
                    return false;
                }
            }
            public bool IsValid(out string nMsg)
            {
                return IsValid(userID, userPW, out nMsg);
            }
        }

    webservice穿插引用soapHeader:

        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        // [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
            public MySoapHeader myheader = new MySoapHeader();
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
    
            //该地方是调用SoapHeader地方,注意观察
            [SoapHeader("myheader")]
            [WebMethod(Description="Say Hello My World")]
            public string HelloWorld2()
            {
                string msg = "";
                if (!myheader.IsValid(out msg))
                {
                    return msg;
                }            
                return "Hello World";
            }
        }    

    客服端动态调用Webservice的方法,可以参考上篇博客

    普通添加引用WEBSERVICE调用的方式代码如下:

     static void Main(string[] args)
            {
    
                MyService.WebService1SoapClient cmlent = new MyService.WebService1SoapClient();
                MyService.MySoapHeader hro=new MyService.MySoapHeader ();
                hro.UserId="admin";
                hro.UserPW="admin";
                string rsult=cmlent.HelloWorld2(hro);
                Console.WriteLine(rsult);
                Console.ReadKey();
            }
  • 相关阅读:
    C语言清空输入缓冲区的N种方法对比(转)
    UNIX网络编程——socket的keep-alive(转)
    UNIX网络编程——套接字选项(心跳检测、绑定地址复用)(转)
    UNIX网络编程——客户/服务器心搏函数 (转)
    TCP心跳 | TCP keepAlive(转)
    linux下使用adb查看android手机的logcat
    linux 常用查看设备命令(转)
    Spring AOP 详解
    HDU 2222 AC自动机 裸题
    大声说出我爱你—英语发音学习总结
  • 原文地址:https://www.cnblogs.com/xibei666/p/4635909.html
Copyright © 2020-2023  润新知