.Net調用Java端帶有WS-Security支持的Web Service【親測經過】

作了幾年的開發,今天終於鼓起勇氣開通了博客園。平時都是找各類大牛,看他們的分享博客的解決BUG。從今天起,我也開始分享我學習之路。還望你們多多支持!       html


 

最近收到一個採用Axis2實現的WebService經過.net對接的需求,開始已爲挺簡單的一個事,常規引用調用後,測試不經過,須要驗證密碼。以後。。。開始瘋狂的尋碼之旅。web

方案一:http://www.javashuo.com/article/p-uufvjqst-ch.html  app

 本方案讓我試的好苦,WSE 2.0、WSE 3.0 都是vs老版本的(本人用VS 2017 ),我嘗試了一下WSE 3.0,沒有成功。你們能夠試試,VS2017 NuGET包裏還有WSE 3.0,應該是能夠的。less

本人方案:ide

                參考地址1:https://blog.csdn.net/yiwenbiao68/article/details/46909395工具

                參考地址2: https://stackoverflow.com/questions/734355/clueless-about-how-to-create-soap-wssesecurity-header學習

        參考地址3:https://blog.csdn.net/oscar999/article/details/40340819測試

首先本人採用的是WCF配置方案。 參考地址1 解決了WSDL地址如何經過WCF工具 生成客戶端可調用的.cs文件。spa

 操做步驟:.net

  1. 打開VS 2017的開發人員命令提示符工具
  2. 輸入svcutil.exe http://localhost:6054/ServiceDemo.svc?wsdl ,直接回車會生成兩個文件.cs、.config。
  3. .cs文件直接拷貝到項目中,.config改爲你項目的app.config替換。打開app.config文件
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="data1Binding" />
                </basicHttpBinding>
                <customBinding>
                    <binding name="data2Binding">
                        <textMessageEncoding messageVersion="Soap12" />
                        <httpTransport />
                    </binding>
                </customBinding>
            </bindings>
            <client>
                <endpoint address="http://XXX1.com/"
                    binding="basicHttpBinding" bindingConfiguration="data1Binding"
                    contract="dataServicePortType" name="dataServiceHttpSoap11Endpoint" />
                <endpoint address="http://XXX2.com/"
                    binding="customBinding" bindingConfiguration="data2Binding"
                    contract="dataServicePortType" name="dataServiceHttpSoap12Endpoint" />
            </client>
        </system.serviceModel>
    </configuration>

    採坑一:有點可能和我同樣有兩個地址。那對不起只能用一個,刪除一個便可。
    採坑二:若是你的WCF作爲一個單項目,要將.config文件內容添加到主項目的.config文件中。

  4. 添加一個SecurityHeader類,代碼以下:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Code
    {
        public class SecurityHeader : System.ServiceModel.Channels.MessageHeader
        {
            public string userName;
            public string password;
    
            public SecurityHeader(string name,string psw) {
                userName = name;
                password = psw;
            }
    
            protected override void OnWriteStartHeader(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion messageVersion)
            {
                writer.WriteStartElement("wsse", Name, Namespace);
                writer.WriteXmlnsAttribute("wsse", Namespace);
            }
    
            protected override void OnWriteHeaderContents(System.Xml.XmlDictionaryWriter writer, System.ServiceModel.Channels.MessageVersion messageVersion)
            {
                writer.WriteStartElement("wsse", "UsernameToken", Namespace);
    
                writer.WriteStartElement("wsse", "Username", Namespace);
                writer.WriteValue(userName);
                writer.WriteEndElement();
    
                writer.WriteStartElement("wsse", "Password", Namespace);
                writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
                writer.WriteValue(password);
                writer.WriteEndElement();
                writer.WriteEndElement();
    
            }
    
            public override string Name
            {
                get { return "Security"; }
            }
    
            public override string Namespace
            {
                get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
            }
        }
    }

    此類主要做用是手動建立了SOAP協議請求頭,參考地址2 爲原創。參考地址3 是我找到的JAVA版代碼,應該能很好的解釋爲何要這麼寫。

  5. 調用方法,代碼以下:
    public static void Main(string[] args)
        {
    
            var webService = new ServiceReference1.MyWebService();//你的webservice
                webService.Open();
    
    
            using (OperationContextScope scope = new OperationContextScope((IContextChannel)webService.InnerChannel))
            {
    
                MessageHeaders messageHeadersElement = OperationContext.Current.OutgoingMessageHeaders;
                messageHeadersElement.Add(new SecurityHeader("UserName", "Password"))
    
    
                 var res = webService.MyServe("方法參數");//webservice 裏的方法
                Console.WriteLine(res.ToString());
            }
        }

 這樣就能夠生成測試了,是否是感受很簡單。可我就是爲此事作了三天。第一次寫,寫的很差的地方,你們請諒解!

相關文章
相關標籤/搜索