【问题标题】:Adding SOAP:HEADER username and password with WSE 3.0使用 WSE 3.0 添加 SOAP:HEADER 用户名和密码
【发布时间】:2010-01-26 12:02:09
【问题描述】:

我已经成功创建了一个在不使用身份验证时可以正常工作的 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.* 命名空间,但我目前不知道如何继续。

客户端代码是由网络参考自动生成的,所以我只做少量工作将消息发送到服务器未经身份验证

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

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

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

谢谢!

【问题讨论】:

    标签: c# web-services ws-security wse3.0


    【解决方案1】:

    确保您的代理类继承自 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 所做的工作。希望对您有所帮助。

    【讨论】:

    • +1 非常有用。在 VS2010 上使用 WseWsdl3.exe 生成代理时出现问题,已通过在注册表中添加条目来解决此问题。在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\ 添加名为“sdkInstallRootv2.0”的字符串键,值为“C:\Program Files\Microsoft SDKs\Windows\v6.0A”
    【解决方案2】:

    不幸的是,在阅读 wsanville's great answer 之前,它开始工作了。

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

    • 安装WSE 3.0,选择自定义并选择所有内容
    • 阅读Implementing direct authentication with username token in 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 服务器响应 表示消息寻址属性的必需标头不存在,并检查传出消息(使用好工具 Fiddler)我从服务器指示缺少 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(...)
    

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

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 2020-07-08
      • 2015-01-21
      相关资源
      最近更新 更多