2010-01-26 136 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