本文主要描述使用.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的引用。.
首先,导入有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