.NetCore调用带密码验证的Webservice

DotNetCore用工具生成的代理类按下图设置用户密码访问webservice一直报用户密码验证不通过。
在这里插入图片描述
查了几天资料都无法突破。为此回到webservice的soap协议的本身来解决。soap的本质就是发布http服务接收按soap文档约定的xml串,然后按xml执行指定方法后返回xml个http调用客户端。然后就用httpclient来调用webservice就行了。

首先下载SOAPUI工具可以查看webservice请求的xml格式,还能测试调用,写代码前可以先用xml测试调用通了再写实现代码。
在这里插入图片描述
带用户密码验证的请求示例:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
    xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <soap:Header>
        <wsa:Action>http://tempuri.org/LIS.WS.DHCLISService.GetData</wsa:Action>
        <wsa:ReplyTo>
            <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
        </wsa:ReplyTo>
        <wsa:To>http://127.0.0.1:57772/imedicallis/csp/LIS.WS.DHCLISService.cls</wsa:To>
        <wsse:Security soap:mustUnderstand="1">
            <wsse:UsernameToken
                xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-29db7c92-1098-4717-b7fb-aac473632fec">
                <wsse:Username>_SYSTEM1</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SYS</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soap:Header>
    <soap:Body>
        <GetData xmlns ="http://tempuri.org">
            <ClassName>Service.LIS.QC.DHCQCService</ClassName>
            <FuncName >GetMachineList</FuncName>
            <Param >
               
            </Param>
            <Session ></Session>
        </GetData >
    </soap:Body>
</soap:Envelope>

也能把调用服务的请求端口和TCPTrace工具配置的监听端口一致然后用这个工具抓包看别的程序成功请求的提交XML串。基于这个串再到SOAPUI测试直到测通。
在这里插入图片描述
然后就实现DotNetCore的调用代理类即可

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Xml;

namespace LIS.DAL.DataAccess
{
    ///<summary  NoteObject="Class">
    /// [功能描述: 按soap协议自定义调用webservice客户端]<br></br>
    /// [创建者:   张联珠]<br></br>
    /// [创建时间: 2021-3-12]<br></br>
    /// <说明>
    ///    
    /// </说明>
    /// <修改记录>
    ///     <修改时间></修改时间>
    ///     <修改内容>
    ///            
    ///     </修改内容>
    /// </修改记录>
    /// </summary>
    public class MyWebserviceClient
    {
        /// <summary>
        /// 服务地址
        /// </summary>
        private string Address = "";

        /// <summary>
        /// 用户名
        /// </summary>
        private string UserName = "";

        /// <summary>
        /// 用户密码
        /// </summary>
        private string UserPass = "";

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="address"></param>
        /// <param name="userName"></param>
        /// <param name="userPass"></param>
        public MyWebserviceClient(string address, string userName, string userPass)
        {
            Address = address;
            UserName = userName;
            UserPass = userPass;
        }

        /// <summary>
        /// 查询M得到数据
        /// </summary>
        /// <param name="ClassName">类名</param>
        /// <param name="FuncName">方法名</param>
        /// <param name="Param">参数</param>
        /// <param name="Session">会话</param>
        /// <returns></returns>
        public string GetDataAsync(string ClassName, string FuncName, string Param, string Session)
        {
            string result = string.Empty;
            try
            {
                string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                xml += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">";
                xml += "<soap:Header>";
                xml += "<wsa:Action>http://tempuri.org/LIS.WS.DHCLISService.GetData</wsa:Action>";
                xml += "<wsa:ReplyTo>";
                xml += "<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>";
                xml += "</wsa:ReplyTo> ";
                xml += "<wsa:To>" + Address + "</wsa:To>";
                xml += "<wsse:Security soap:mustUnderstand=\"1\">";
                xml += "<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"SecurityToken-29db7c92-1098-4717-b7fb-aac473632fec\">";
                xml += "<wsse:Username>" + UserName + "</wsse:Username>";
                xml += "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">" + UserPass + "</wsse:Password>";
                xml += "</wsse:UsernameToken>";
                xml += "</wsse:Security>";
                xml += "</soap:Header>";
                xml += "<soap:Body>";
                xml += "<GetData xmlns =\"http://tempuri.org\">";
                xml += "<ClassName>" + ClassName + "</ClassName>";
                xml += "<FuncName >" + FuncName + "</FuncName>";
                xml += "<Param ><![CDATA[" + Param + "]]></Param>";
                xml += "<Session >" + Session + "</Session>";
                xml += "</GetData >";
                xml += "</soap:Body>";
                xml += "</soap:Envelope>";
                HttpContent content = new StringContent(xml, Encoding.UTF8, "text/xml");
                content.Headers.Add("SOAPAction", "http://tempuri.org/LIS.WS.DHCLISService.GetData");
                using (HttpClient client = new HttpClient())

                using (var response = client.PostAsync(Address, content))
                {
                    result = response.Result.Content.ReadAsStringAsync().Result;
                    //创建一个xml文档
                    XmlDocument xmlDoc = new XmlDocument();
                    //为文档导入数据
                    xmlDoc.LoadXml(result);
                    result = xmlDoc.InnerText;
                    //调用报错了
                    if (!result.Contains("<Response>"))
                    {
                        result = "<Response><SQLResult><SQL><FunRet></FunRet></SQL></SQLResult><RetVal>-1</RetVal><Error>" + result + "</Error><Node></Node><RowCount>0</RowCount></Response>";
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 查询M得到SQL执行结果
        /// </summary>
        /// <param name="SQLText">SQL串</param>
        /// <param name="Param">参数</param>
        /// <param name="Session">会话</param>
        /// <returns>返回串</returns>
        public string GetSQLDataAsync(string SQLText, string Param, string Session)
        {
            string result = string.Empty;
            try
            {
                string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
                xml += "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">";
                xml += "<soap:Header>";
                xml += "<wsa:Action>http://tempuri.org/LIS.WS.DHCLISService.GetSQLData</wsa:Action>";
                xml += "<wsa:ReplyTo>";
                xml += "<wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>";
                xml += "</wsa:ReplyTo> ";
                xml += "<wsa:To>" + Address + "</wsa:To>";
                xml += "<wsse:Security soap:mustUnderstand=\"1\">";
                xml += "<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"SecurityToken-29db7c92-1098-4717-b7fb-aac473632fec\">";
                xml += "<wsse:Username>" + UserName + "</wsse:Username>";
                xml += "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">" + UserPass + "</wsse:Password>";
                xml += "</wsse:UsernameToken>";
                xml += "</wsse:Security>";
                xml += "</soap:Header>";
                xml += "<soap:Body>";
                xml += "<GetSQLData xmlns = \"http://tempuri.org\">";
                xml += "<SQLText>" + SQLText + "</SQLText>";
                xml += "<Param>" + Param + "</Param >";
                xml += "<Session>" + Session + "</Session>";
                xml += "</GetSQLData>";
                xml += "</soap:Body>";
                xml += "</soap:Envelope>";
                HttpContent content = new StringContent(xml, Encoding.UTF8, "text/xml");
                content.Headers.Add("SOAPAction", "http://tempuri.org/LIS.WS.DHCLISService.GetSQLData");
                using (HttpClient client = new HttpClient())

                using (var response = client.PostAsync(Address, content))
                {
                    result = response.Result.Content.ReadAsStringAsync().Result;
                    //创建一个xml文档
                    XmlDocument xmlDoc = new XmlDocument();
                    //为文档导入数据
                    xmlDoc.LoadXml(result);
                    result = xmlDoc.InnerText;
                    //调用报错了
                    if(!result.Contains("<Response>"))
                    {
                        result = "<Response><SQLResult><SQL><FunRet></FunRet></SQL></SQLResult><RetVal>-1</RetVal><Error>"+result+"</Error><Node></Node><RowCount>0</RowCount></Response>";
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }
    }
}

这样就可以调通了额,适应所有webservice调用情况,要点就是回归soap协议,用httpclient发soapxml。结合抓包工具和soapui测试。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用 .NET Core Socket 进行 SSL/TLS 通信时,可以使用 X509Certificate2 类来验证证书。具体步骤如下: 1. 实例化 X509Certificate2 对象,将证书加载进来。 ```csharp var cert = new X509Certificate2("path/to/certificate.pfx", "password"); ``` 2. 创建 SslStream 对象,并在构造函数中指定套接字和是否进行身份验证。 ```csharp var sslStream = new SslStream(socket, false, ValidateCertificate); ``` 第二个参数指定是否在 SSL/TLS 握手期间进行身份验证。如果指定为 true,则会在握手期间验证服务器证书和客户端证书(如果有)的有效性。如果指定为 false,则可以在握手之后显式地调用 ValidateCertificate 方法来验证证书。 3. 在 ValidateCertificate 方法中,可以使用 X509Chain 类来验证证书链的有效性。 ```csharp private bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { if (sslPolicyErrors == SslPolicyErrors.None) { // 证书链有效 return true; } // 验证证书链的有效性 var chainPolicy = new X509ChainPolicy(); chainPolicy.RevocationMode = X509RevocationMode.NoCheck; chainPolicy.VerificationFlags = X509VerificationFlags.IgnoreUnknownAuthority | X509VerificationFlags.IgnoreEndRevocationUnknown; if (!chain.Build(new X509Certificate2(certificate))) { // 证书链无效 return false; } // 验证证书链中的每个证书是否被信任 foreach (var chainElement in chain.ChainElements) { if (!chainElement.Certificate.Verify()) { // 证书无效 return false; } } // 证书链有效 return true; } ``` 在上面的代码中,我们首先检查 sslPolicyErrors 参数,如果为 SslPolicyErrors.None,则表示证书链是有效的,可以直接返回 true。 如果 sslPolicyErrors 不为 SslPolicyErrors.None,则需要使用 X509Chain 类来验证证书链的有效性。我们创建了一个 X509ChainPolicy 对象,并指定了 RevocationMode 和 VerificationFlags 属性。然后,我们调用 chain.Build 方法来构建证书链,并检查结果是否为 true。如果结果为 false,则证书链是无效的,返回 false。 如果证书链是有效的,则需要验证证书链中的每个证书是否被信任。我们使用 foreach 循环遍历证书链中的每个证书,并调用 Verify 方法来验证证书是否被信任。如果有任何一个证书无效,则返回 false。如果所有证书都被信任,则返回 true,表示证书链是有效的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小乌鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值