• 在WebService中使用简单的自定义SoapHeader


    1.新建专案及加入Web服务 WebServiceA
    2.在WebServiceA.cs中加入新的public class MySOAPHeader
    3.修改WebServiceA的HelloWorld() 添加SoapHeader
    4.加入Web引用 及 新建测试页面
    5.测试页面的按钮事件
    ================================

    1.新建专案及加入Web服务 WebServiceA

    2.在WebServiceA.cs中加入新的public class MySOAPHeader
    -----------------------------------------------------
    public class MySOAPHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string UserPWD;
        public string UserName;

        public bool IsValidUser()
        {
            string strUserName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userName", "md5");
            string strUserPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("userPWD", "md5");
            if ( (UserName == strUserName) && (UserPWD == strUserPWD))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    3.修改WebServiceA的HelloWorld() 添加SoapHeader
    ----------------------------------------------
    /// <summary>
    /// WebServiceA 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebServiceA : System.Web.Services.WebService {

        public WebServiceA ()
        {
            //如果使用设计的组件,请取消注释以下行
            //InitializeComponent();
        }

        public MySOAPHeader userSOAPHeader ;

        [WebMethod, SoapHeader("userSOAPHeader")]   
        public string HelloWorld()
        {
            if (userSOAPHeader.IsValidUser())
            {
                return "Hello World";
            }
            else
            {
                return "Sorry";
            }
        }   
    }


    4.加入Web引用 及 新建测试页面

    5.测试页面的按钮事件
    -------------------
    protected void Button1_Click(object sender, EventArgs e)
    {
        //
        string strUserName = this.txt_UserName.Text.Trim();
        strUserName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserName, "md5");
        string strUserPWD = this.txt_UserPWD.Text.Trim();
        strUserPWD = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strUserPWD, "md5");
        //
        localhost.WebServiceA wsA = new localhost.WebServiceA();
        //
        localhost.MySOAPHeader userSOAPHeader = new localhost.MySOAPHeader();
        userSOAPHeader.UserName = strUserName;
        userSOAPHeader.UserPWD = strUserPWD;
        //
        wsA.MySOAPHeaderValue = userSOAPHeader;
        string strReturn = wsA.HelloWorld();
        //
        this.TextBox1.Text = strReturn;
    }

  • 相关阅读:
    Element-UI 表单验证规则rules 配置参数说明
    vue中 关于$emit的用法
    IEDA 自动生成类注释和方法注释
    maven配置多个镜像
    git 查看修改用户名,密码
    cnpm的安装(超级详细版)
    windows命令行中 启动应用程序
    通用遥控编码2262与1527解说
    Zeller Format
    字符串截取算法
  • 原文地址:https://www.cnblogs.com/freeliver54/p/763546.html
Copyright © 2020-2023  润新知