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

csl*_*csl 8 c# ws-security web-services wse3.0

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

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

<soapenv:Envelope 
  xmlns:ns="http://foo.bar/1.0"
  xmlns:ns1="http://www.witsml.org/schemas/140"   
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

  <soapenv:Header>

    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken wsu:Id="UsernameToken-2" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>foo</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">bar</wsse:Password>    
        <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">foooooobar==</wsse:Nonce>
        <wsu:Created>2010-01-25T13:09:24.860Z</wsu:Created>
      </wsse:UsernameToken>
    </wsse:Security>

  </soapenv:Header>

  <soapenv:Body>
    <ns:fooBar>...</ns:fooBar>
  </soapenv:Body>
Run Code Online (Sandbox Code Playgroud)

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

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

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

WS.FooResultHttpService ws = new WS.FooResultHttpService();
ws.Url = "http://foo.bar.baz";
ws.SendSomething(message);
Run Code Online (Sandbox Code Playgroud)

我刚开始调查使用Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager,但到目前为止,我还没有能够得到任何东西.

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

谢谢!

wsa*_*lle 14

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

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

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

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));
Run Code Online (Sandbox Code Playgroud)

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


csl*_*csl 11

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

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

  • 安装WSE 3.0,选择自定义并选择所有内容
  • 阅读在WSE 3.0中使用用户名令牌实现直接身份验证以获取提示
  • 重新启动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)
Run Code Online (Sandbox Code Playgroud)

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

using Microsoft.Web.Services3.Design;

// ...

public class FooBarPolicy : Policy
{
    public FooBarPolicy()
    {
        this.Assertions.Add(new UsernameOverTransportAssertion());
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

using Microsoft.Web.Services3.Addressing;

// ...

wse.RequestSoapContext.Addressing.Action = new Action("CallSomeServerFunction");
Run Code Online (Sandbox Code Playgroud)

问题是,即使我设置了一个动作,当它通过电线发送时,它也是空的.原来我必须打开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(...)
Run Code Online (Sandbox Code Playgroud)

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