2010-01-26 142 views
7

我已经成功创建了一个无需使用身份验证即可正常工作的WS客户端。使用WSE 3.0添加SOAP:HEADER用户名和密码

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

我已经下载并安装微软的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,但到目前为止,我还没有能够得到任何东西和运行。

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

谢谢!

回答

10

得知它的工作,不幸的是在阅读wsanville's great answer之前。

去帮助别人,我张贴所有我需要做的步骤得到它与Visual Studio 2005的工作:

  • 安装WSE 3.0,选择定制并选择一切
  • Implementing direct authentication with username token in WSE 3.0为提示
  • 重新启动Visual Studio 2005,现在在解决方案资源管理器中右键单击您的项目,并且您应该有一个WSE设置3.0菜单项并使用它,如果您想要的话。
  • 更新您的网站引用,这应该创建一个新的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(...) 

在此之后,它的所有工作出很好。

12

确保您的代理类继承自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在2008年工作室希望帮助去。

+0

谢谢,这非常有帮助! – csl 2010-01-26 14:44:03

+0

+1非常有用。如果在VS2010上使用WseWsdl3.exe生成代理问题,则通过在注册表中添加条目来解决此问题。在HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ .NETFramework \中添加名为“sdkInstallRootv2.0”的字符串键值为“C:\ Program Files \ Microsoft SDKs \ Windows \ v6.0A” – Siva 2012-04-02 13:52:13