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

发布于 2024-08-19 01:24:53 字数 1805 浏览 5 评论 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>

我已经下载并安装了 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,但到目前为止我还无法启动并运行任何东西。

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

谢谢!

I have successfully created a WS client that works correctly when NOT using authentication.

However, the server (WebSphere) now requires adding a ws-security username token, and I'm having a hard time doing this. The resulting SOAP message is supposed to look something like this:

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

I've downloaded and installed Microsoft's WSE 3.0 SDK and added a reference to the DLL in my Visual Studio 2005 project.

I now have access to the Microsoft.Web.Services3.* namespaces, but I'm currently stumped on how to proceed.

The client code has been generated automatically by a web reference, so I only do a minor amount of work to send the message to the server unauthenticated:

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

I've just begun to investigate using Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager, but so far I haven't been able to get anything up and running.

Any hints would be greatly appreciated, as I can't seem to find any good recipes on the net.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-08-26 01:24:53

确保您的代理类继承自 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));

这就是我过去为让 WSE3.0 在 Studio 2008 中运行而所做的事情。希望有所帮助。

Make sure your proxy class inherits from Microsoft.Web.Services3.WebServicesClientProtocol.

You can do this either by changing the proxy class itself, or by generating it via the command line using wsewsdl3.exe with the /type:webClient switch.

You can then pass the credentials like this:

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

This is what I've done in the past to get WSE3.0 going in Studio 2008. Hope that helps.

朱染 2024-08-26 01:24:53

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

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

  • 安装 WSE 3.0,选择自定义并选择所有内容
  • 阅读在 WSE 3.0 中使用用户名令牌实现直接身份验证
  • 重新启动 Visual Studio 2005,现在右键单击解决方案资源管理器,并且您应该有一个 WSE 设置 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(...)

之后,一切都很顺利。

Got it working, unfortunately before reading wsanville's great answer.

To help others, I'm posting all the steps I needed to do to get it working with Visual Studio 2005:

  • Install WSE 3.0, choose custom and select everything
  • Read Implementing direct authentication with username token in WSE 3.0 for hints
  • Relaunch Visual Studio 2005, now right-click on your project in the solution explorer, and you should have a WSE Settings 3.0 menu item and use that if you want to.
  • Update your web references, this should create a new HTTP web service proxy class, with a different name, e.g. YourWsNameHttpServiceWse. This is essentially the same as running wsewsdl3.exe
  • Use this new class, and you should have access to WSE methods and properties, such as SetClientCredential.

I ended up doing almost everything in code, instead of relying on the config-files that are built with my C# DLL. The code ended up looking like this:

FooBarHttpServiceWse wse = new FooBarHttpServiceWse();

wse.SetClientCredential(new UsernameToken(
    "username",
    "password",
    PasswordOption.SendPlainText));

wse.SetPolicy(new FooBarPolicy());
wse.CallSomeServerFunction(yourRequest)

I created my own policy, which looked like this:

using Microsoft.Web.Services3.Design;

// ...

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

Finally, the WebSphere server responded that A required header representing a Message Addressing Property is not present, and inspecting the outgoing message (using the nice tool Fiddler) I saw the SOAP fault from the server indicated that the Action header was missing.

I tried in vain to set the wsa:Action element myself:

using Microsoft.Web.Services3.Addressing;

// ...

wse.RequestSoapContext.Addressing.Action = new Action("CallSomeServerFunction");

The problem was that even if I set an action, when it was sent over the wire, it was empty. Turned out I had to open the WSE proxy class and edit an attribute there:

[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(...)

After that, it all worked out nicely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文