c# WebService添加SoapHeader认证

1.添加一个cretificate类继承自SoapHeader
     public class CertificateSoapHeader: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 ValideUser(string in_UserName, string in_PassWord)
            {
                string cofig_user = ConfigHelper.GetAppSettings("soapUser");
                string config_pwd = ConfigHelper.GetAppSettings("soapPwd");
                if ((in_UserName == cofig_user) && (in_PassWord == config_pwd))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
2.自写的WebService方法中添加[SoapHeader("soapHeader")]
 public class Settle_Pay : System.Web.Services.WebService
    {
        public CertificateSoapHeader soapHeader = new CertificateSoapHeader();
        [WebMethod(EnableSession = true, Description = "更新审批状态")]
        [SoapHeader("soapHeader")]//添加认证标头
        public Settle_PayReturnEntity UpdateSettlePayApproval(string OA_Id, string status)
        {
            Settle_PayReturnEntity returnEntity = new Settle_PayReturnEntity();
         //校验用户名密码
            if (!soapHeader.ValideUser(soapHeader.UserName, soapHeader.Password))
            {
                returnEntity.Type = "E";
                returnEntity.Message = "用户名密码错误";
                return returnEntity;
            }
            List<StringBuilder> sqls = new List<StringBuilder>();
            List<object> objs = new List<object>();
            sqls.Add(new StringBuilder(@" update Settle_Pay set applyStatus='" + status + "' where OA_Id='" + OA_Id + "'"));
            objs.Add(null);

            string err = string.Empty;
            int r = DataFactory.SqlDataBase().BatchExecuteByListSql(sqls, objs, ref err);
            if (r > 0)
            {
                returnEntity.Type = "S";
                returnEntity.Message = "审核通过!";
            }
            else
            {
                returnEntity.Type = "E";
                returnEntity.Message = err;
            }
            return returnEntity;
        }

    }
3.通过SoapUI验证

 



 

转载于:https://www.cnblogs.com/lbja2/p/9647183.html

C#中调用WebService添加SOAPHeader头信息的方法有多种。一种简单的方法是在具体的WebService方法上添加特性\[SoapHeader("RequestSOAPHeader")\]\[1\]。另一种方法是通过编写代码来发送HTTP POST请求添加SOAPHeader头信息\[2\]\[3\]。你可以使用WebRequest类创建一个WebRequest对象,设置ContentType为"text/xml; charset=utf-8",设置Method为"POST",并添加SOAPAction头信息。然后使用GetRequestStream方法获取请求流,将参数转换为字节数组并写入请求流中。最后,使用GetResponse方法获取响应,并使用StreamReader读取响应流中的数据。这样就可以得到调用WebService后的结果。 #### 引用[.reference_title] - *1* [C#WebService客户端,添加SOAPHeader信息。](https://blog.csdn.net/catzhangzbt/article/details/112779826)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [C#调用Java的WebService添加SOAPHeader验证](https://blog.csdn.net/weixin_30636089/article/details/99554547)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值