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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2139190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:49:58  来源:igfitidea点击:

Adding SOAP:HEADER username and password with WSE 3.0

c#web-servicesws-securitywse3.0

提问by csl

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

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

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:

但是,服务器 (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>

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

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

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

我现在可以访问 Microsoft.Web.Services3.* 命名空间,但我目前对如何继续感到困惑。

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.

我刚刚开始研究使用Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager,但到目前为止我还没有能够启动和运行任何东西。

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

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

Thanks!

谢谢!

采纳答案by csl

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

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

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

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

  • Install WSE 3.0, choose customand select everything
  • Read Implementing direct authentication with username token in WSE 3.0for hints
  • Relaunch Visual Studio 2005, now right-click on your project in the solution explorer, and you should have a WSE Settings 3.0menu 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.
  • 安装WSE 3.0,选择自定义并选择所有内容
  • 阅读在 WSE 3.0 中使用用户名令牌实现直接身份验证以获取提示
  • 重新启动 Visual Studio 2005,现在在解决方案资源管理器中右键单击您的项目,您应该有一个WSE Settings 3.0菜单项,如果需要,可以使用它。
  • 更新您的 web 引用,这应该创建一个新的 HTTP web 服务代理类,具有不同的名称,例如YourWsNameHttpServiceWse. 这与运行wsewsdl3.exe基本相同
  • 使用这个新类,您应该可以访问 WSE 方法和属性,例如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:

我最终几乎用代码完成了所有工作,而不是依赖于用我的 C# DLL 构建的配置文件。代码最终看起来像这样:

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.

最后,WebSphere 服务器响应A required header 表示 Message Addressing Property is not present,并且检查传出消息(使用不错的工具Fiddler)我看到来自服务器的 SOAP 错误表明缺少 Action 标头。

I tried in vain to set the wsa:Actionelement myself:

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

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:

问题是,即使我设置了一个动作,当它通过网络发送时,它也是空的。结果我不得不打开 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(...)

After that, it all worked out nicely.

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

回答by wsanville

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

确保您的代理类继承自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.exewith the /type:webClientswitch.

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

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.

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