添加SOAP:使用WSE 3.0的HEADER用户名和密码

我已经成功创建了一个在不使用身份validation时正常工作的WS客户端。

但是,服务器(WebSphere)现在需要添加一个ws-security用户名令牌,而我很难做到这一点。 生成的SOAP消息应该看起来像这样:

    foo bar foooooobar== 2010-01-25T13:09:24.860Z     ...  

我已下载并安装了Microsoft的WSE 3.0 SDK,并在我的Visual Studio 2005项目中添加了对DLL的引用。

我现在可以访问Microsoft.Web.Services3。*命名空间,但我目前难以理解如何继续。

客户端代码是由Web引用自动生成的,因此我只做了少量工作就将消息发送到未经身份validation的服务器:

 WS.FooResultHttpService ws = new WS.FooResultHttpService(); ws.Url = "http://foo.bar.baz"; ws.SendSomething(message); 

我刚刚开始使用Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager进行调查,但到目前为止我还没有能够获得任何启动和运行。

任何提示将不胜感激,因为我似乎无法在网上找到任何好的食谱。

谢谢!

确保您的代理类inheritance自Microsoft.Web.Services3.WebServicesClientProtocol

您可以通过更改代理类本身,或使用带有/type:webClient开关的wsewsdl3.exe通过命令行生成它来执行此操作。

然后,您可以传递这样的凭据:

 using Microsoft.Web.Services3; using Microsoft.Web.Services3.Security.Tokens; using Microsoft.Web.Services3.Security; . . . WS.FooResultHttpService ws = new WS.FooResultHttpService(); ws.RequestSoapContext.Security.Tokens.Add(new UsernameToken("blah", "blah", PasswordOption.SendPlainText)); 

这就是我过去在Studio 2008中使用WSE3.0所做的事情。希望有所帮助。

不幸的是,在阅读wsanville的好答案之前,它已经工作了。

为了帮助其他人,我发布了我需要做的所有步骤,以使其与Visual Studio 2005一起使用:

  • 安装WSE 3.0 ,选择自定义并选择所有内容
  • 阅读在WSE 3.0中使用用户名令牌实现直接身份validation以获取提示
  • 重新启动Visual Studio 2005,现在右键单击解决方案资源管理器中的项目,您应该有一个WSE Settings 3.0菜单项,如果您愿意,可以使用它。
  • 更新您的Web引用,这应该创建一个具有不同名称的新HTTP Web服务代理类,例如YourWsNameHttpServiceWse 。 这与运行wsewsdl3.exe基本相同
  • 使用这个新类,您应该可以访问WSE方法和属性,例如SetClientCredential

我最终在代码中做了几乎所有事情,而不是依赖于使用我的C#DLL构建的配置文件。 代码最终看起来像这样:

 FooBarHttpServiceWse wse = new FooBarHttpServiceWse(); wse.SetClientCredential(new UsernameToken( "username", "password", PasswordOption.SendPlainText)); wse.SetPolicy(new FooBarPolicy()); wse.CallSomeServerFunction(yourRequest) 

我创建了自己的策略,如下所示:

 using Microsoft.Web.Services3.Design; // ... public class FooBarPolicy : Policy { public FooBarPolicy() { this.Assertions.Add(new UsernameOverTransportAssertion()); } } 

最后,WebSphere服务器响应, 表示消息寻址属性的必需标头不存在 ,并检查传出消息(使用漂亮的工具Fiddler )我从服务器看到SOAP错误表明缺少Action标头。

我徒劳地尝试设置wsa:Action元素:

 using Microsoft.Web.Services3.Addressing; // ... wse.RequestSoapContext.Addressing.Action = new Action("CallSomeServerFunction"); 

问题是,即使我设置了一个动作,当它通过电线发送时,它也是空的。 原来我必须打开WSE代理类并在那里编辑一个属性:

 [System.Web.Services.Protocols.SoapDocumentMethodAttribute( "---Edit this to set wsa:Action---", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)] // ... public SomeServerFunction(...) 

在那之后,一切都很顺利。