8

我已经成功创建了一个在不使用身份验证时可以正常工作的 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>

我已经下载并安装了 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);

我刚刚开始使用 进行调查Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager,但到目前为止,我还无法启动并运行任何东西。

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

谢谢!

4

2 回答 2

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));

这是我过去为在 Studio 2008 中使用 WSE3.0 所做的工作。希望对您有所帮助。

于 2010-01-26T13:06:32.077 回答
12

不幸的是,在阅读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)

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

using Microsoft.Web.Services3.Design;

// ...

public class FooBarPolicy : Policy
{
    public FooBarPolicy()
    {
        this.Assertions.Add(new UsernameOverTransportAssertion());
    }
}

最后,WebSphere 服务器响应A required header代表 Message Addressing Property is not present,并检查传出消息(使用漂亮的工具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(...)

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

于 2010-01-26T14:41:55.150 回答