本文主要描述使用.net客戶端調用java寫的服務端的webservice,並且使用了WSI協議中的UserNameToken驗證方法。
先給出要POST的包格式:
- <soap:Envelope xmlns:soap="…">
- <soap:Header>
- <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
- <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-7f2cd499-d67d-4052-87c3-870833c2fb06">
- <wsse:Username>much</wsse:Username>
- <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">123456</wsse:Password>
- </wsse:UsernameToken>
- </wsse:Security>
- <RequestSOAPHeader xmlns="….">
- < otherheader>xxxx</otherheader>
- </RequestSOAPHeader>
- </soap:Header>
- <soap:Body>
- ……..
- </soap:Body>
- </soap:Envelope>
安裝WSE3.0版本。在VS項目中添加Microsoft.Web.Services3的引用。.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
首先,導入有java服務端生成的wsdl文件,生成客戶段代理類。
這裏導入wsdl文件有兩種方法:
一、在VS提供的命令提示符中編譯WSDL文件。
給個例子:
Wsdl /language:CS /n:mynamespace /out:myProxyClass.cs C:/myProject/wsdl/webservice.wsdl
最後一個參數是本地的絕對路徑,是一個文件,也可以是一個網絡路徑。
二、在項目右鍵中添加WEB引用,輸入本地的WSDL的絕對路徑。
注意:用VS引用生成的代理類名稱爲Reference.cs,可以在項目目錄下找到。
然後,修改本地生成的代理類,在最後一個using下面添加
- [System.Xml.Serialization.XmlTypeAttribute(Namespace = "…")]
- [System.Xml.Serialization.XmlRootAttribute(Namespace = "…", IsNullable = false)]
- public class RequestSOAPHeader : System.Web.Services.Protocols.SoapHeader
- {
- public string otherheader;
- }
將System.Web.Services.Protocols.SoapHttpClientProtocol替換爲Microsoft.Web.Services3.WebServicesClientProtocol
在代理類的構造函數上面,private bool useDefaultCredentialsSetExplicitly這行代碼下面添加public RequestSOAPHeader ServiceAuthHeaderValue這行代碼,注意要在類的裏面,作爲服務類的公共對象。
最後,找到你要調用的那個服務方法,在上面添加
[System.Web.Services.Protocols.SoapHeaderAttribute("ServiceAuthHeaderValue")]這行代碼,注意ServiceAuthHeaderValue是聲明的公共成員。
如果你找不到,你可以搜索下類似[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]的代碼。對,就在這上面或下面添加。
如果你看了上面的這些還不是很理解,可以參考下面的例子(根據一個網友提供的資料整理,省略部分代碼)
- sing System;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Serialization;
- //
- // 此源代碼由 wsdl 自動生成, Version=2.0.50727.1432。
- //
- [System.Xml.Serialization.XmlTypeAttribute(Namespace = "…")]
- [System.Xml.Serialization.XmlRootAttribute(Namespace = "…", IsNullable = false)]
- public class RequestSOAPHeader : System.Web.Services.Protocols.SoapHeader
- {
- public string otherheader;
- }
- /// <remarks/>
- [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]
- [System.Diagnostics.DebuggerStepThroughAttribute()]
- [System.ComponentModel.DesignerCategoryAttribute("code")]
- [System.Web.Services.WebServiceBindingAttribute(Name="SendMessageBinding", Namespace="http://www.xxx.com.cn")]
- public partial class SendMessageBinding : Microsoft.Web.Services3.WebServicesClientProtocol {
- private System.Threading.SendOrPostCallback sendMessageOperationCompleted;
- private System.Threading.SendOrPostCallback getMessageDeliveryStatusOperationCompleted;
- public RequestSOAPHeader ServiceAuthHeaderValue;
- /// <remarks/>
- public SendMessageBinding() {
- this.Url = "http://www.xxx.com.cn";
- }
- /// <remarks/>
- public event sendMessageCompletedEventHandler sendMessageCompleted;
- /// <remarks/>
- public event getMessageDeliveryStatusCompletedEventHandler getMessageDeliveryStatusCompleted;
- /// <remarks/>
- [System.Web.Services.Protocols.SoapHeaderAttribute("ServiceAuthHeaderValue")]
- [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
- [return: System.Xml.Serialization.XmlElementAttribute("sendMessageResponse", Namespace="http://www.xxx.com.cn")]
- public sendMessageResponse sendMessage([System.Xml.Serialization.XmlElementAttribute("sendMessage", Namespace="http://www.xxx.com.cn")] sendMessage sendMessage1) {
- object[] results = this.Invoke("sendMessage", new object[] {
- sendMessage1});
- return ((sendMessageResponse)(results[0]));
- }
- ......
到這裏就修改好了代理類了,注意保存備份下,不要更新代理類,否則,代理類又會還原回去了。
接着設置下WSE的配置,右擊項目選擇WSE settings 3.0…
勾選General下的Enable this project for web service enhancements
選擇Policy,勾選Enable Policy,然後點Add,我這裏已經添加了,沒添加的時候是空的。
填寫Policy的名稱爲ClientPolicy,在嚮導中,依次選擇Secure a client application+username ->Specify username token in code ->None
最後生成的Policy信息如下:
然後到Security選項卡里點擊Security Tokens Managers中的Add,在下拉框中選擇Username Token Manager,會自動生成其他的信息,點OK保存即可。
看下你的app.config中有沒有如下信息:
- <configSections>
- <section name="microsoft.web.services3" type="Microsoft.Web.Services3.Configuration.WebServicesConfiguration, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
- …….
- </sectionGroup>
- </configSections>
- <microsoft.web.services3>
- <policy fileName="wse3policyCache.config" />
- <security>
- <securityTokenManager>
- <add type="Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" localName="UsernameToken" />
- </securityTokenManager>
- </security>
- </microsoft.web.services3>
wse3policyCache.config裏的信息如下:
- <policies xmlns="http://schemas.microsoft.com/wse/2005/06/policy">
- <extensions>
- <extension name="requireActionHeader" type="Microsoft.Web.Services3.Design.RequireActionHeaderAssertion, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
- <extension name="usernameOverTransportSecurity" type="Microsoft.Web.Services3.Design.UsernameOverTransportAssertion, Microsoft.Web.Services3, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
- </extensions>
- <policy name="ClientPolicy">
- <usernameOverTransportSecurity />
- <requireActionHeader />
- </policy>
- </policies>
下面是調用服務方法的關鍵代碼:
- //服務對象
- ProxyService sendws = new ProxyService();
- //添加SOAP頭
- ProxyService.RequestSOAPHeader Soapheader = new ProxyService.RequestSOAPHeader();
- Soapheader.otherheader=”xxx”;
- sendws.ServiceAuthHeaderValue = Soapheader;
- //設置協議
- UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
- sendws.SetClientCredential(token);
- sendws.SetPolicy("ClientPolicy");
- sendws.Url = ".....";
- sendws.webmethod ();
到這裏使用Fiddler截包,發現SOAP包頭中的信息出現了Action, Timestamp等信息,爲了得到更加靈活的SOAP頭,就必須重寫SoapFilter類。
在你的項目中添加一個類UsernameClientAssertion:
- class UsernameClientAssertion : SecurityPolicyAssertion
- {
- public string UserName;
- public string PassWord;
- public UsernameClientAssertion(string UserName, string PassWord)
- {
- this.UserName = UserName;
- this.PassWord = PassWord;
- }
- public override SoapFilter CreateClientOutputFilter(FilterCreationContext context)
- {
- return new ClientOutputFilter(this, context);
- }
- public override SoapFilter CreateClientInputFilter(FilterCreationContext context)
- {
- return null;
- }
- public override SoapFilter CreateServiceInputFilter(FilterCreationContext context)
- {
- return null;
- }
- public override SoapFilter CreateServiceOutputFilter(FilterCreationContext context)
- {
- return null;
- }
- }
新建一個ClientOutputFilter
- class ClientOutputFilter : SoapFilter
- {
- private UsernameClientAssertion parentAssertion;
- private FilterCreationContext filterContext;
- public ClientOutputFilter(UsernameClientAssertion parentAssertion, FilterCreationContext filterContext)
- {
- this.parentAssertion = parentAssertion;
- this.filterContext = filterContext;
- }
- public override Microsoft.Web.Services3.SoapFilterResult ProcessMessage(SoapEnvelope envelope)
- {
- XmlElement securityElement = envelope.CreateElement("wsse", "Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
- XmlAttribute mustUnderstandAttribute = envelope.CreateAttribute(envelope.DocumentElement.Prefix,
- "mustUnderstand", envelope.DocumentElement.NamespaceURI);
- mustUnderstandAttribute.Value = "true";
- UsernameToken userToken = new UsernameToken(parentAssertion.UserName, parentAssertion.PassWord, PasswordOption.SendPlainText);
- securityElement.AppendChild(userToken.GetXml(envelope));
- securityElement.FirstChild.RemoveChild(securityElement.FirstChild.FirstChild.NextSibling.NextSibling);
- securityElement.FirstChild.RemoveChild(securityElement.FirstChild.FirstChild.NextSibling.NextSibling);
- //envelope.CreateHeader().RemoveAll();
- envelope.CreateHeader().PrependChild(securityElement);
- envelope.CreateHeader().RemoveChild(envelope.CreateHeader().FirstChild.NextSibling.NextSibling);
- envelope.CreateHeader().RemoveChild(envelope.CreateHeader().FirstChild.NextSibling.NextSibling);
- envelope.CreateHeader().RemoveChild(envelope.CreateHeader().FirstChild.NextSibling.NextSibling);
- envelope.CreateHeader().RemoveChild(envelope.CreateHeader().FirstChild.NextSibling.NextSibling);
- return SoapFilterResult.Continue;
- }
- }
簡單說下上面的代碼,創建靈活的SOAP頭就在方法public override Microsoft.Web.Services3.SoapFilterResult ProcessMessage(SoapEnvelope envelope)裏,
可以看得出就是對XML的操作啦,如果調不出你要的包,就設個斷點F10,F10。。。
修改下原來的代碼爲:
- //服務對象
- ProxyService sendws = new ProxyService();
- //添加SOAP頭
- ProxyService.RequestSOAPHeader Soapheader = new ProxyService.RequestSOAPHeader();
- Soapheader.otherheader=”xxx”;
- sendws.ServiceAuthHeaderValue = Soapheader;
- //設置協議
- UsernameToken token = new UsernameToken("username", "password", PasswordOption.SendPlainText);
- Policy ProvideUsernameToken = new Policy();
- ProvideUsernameToken.Assertions.Add(new UsernameClientAssertion("domain_user", "domain_user"));
- UsernameTokenManager tokenm = new UsernameTokenManager();
- sendws.SetClientCredential(token);
- sendws.SetPolicy(ProvideUsernameToken);
- sendws.Url = "...";
- sendws.webmethod ();
這樣就重寫了SOAP頭的輸出部分了。
總結
WSE3.0只能在VS2005下才能集成在項目中,VS2008不能支持,我之前試着直接改寫app.config和wse3policyCache.config裏的內容都失敗了,覺得可能是證書的生成有問題,WSE也提供手動生成證書,但是這麼多東西都用手動生成,恐怕不太容易,稍有差錯就調試不出來了。
在VS2008下已經使用WCF代替了WSE了,使用了綁定設置的方法,我也沒有試出來,如果你試出來了,請告訴我,謝謝。
修訂:
1.添加代理類例子 2008-10-15
2.修改標題,方便搜索的到 2008-11-15