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验证

 



 

posted @ 2018-09-14 16:03  lbja2  阅读(4536)  评论(0编辑  收藏  举报