2011-05-09 13 views
3

我試圖配置我的WCF客戶端來創建包含WS-Addressing,WS-Security和TLS的SOAP 1.1請求。如何配置WCF只簽署TimeStamp報頭

安全要求是消息包含用戶名令牌TimeStamp並且TimeStamp使用包含的BinarySecurityToken進行簽名。

我已經使用下面link的示例來創建我的WCF客戶端綁定。我略微修改了該示例(請參見下文),以便HTTPS用作傳輸機制,而MessageSecurity基於UsernameOverTransport。

  HttpsTransportBindingElement httpsTransport = new HttpsTransportBindingElement();    
     // the message security binding element will be configured to require 2 tokens: 
     // 1) A username-password encrypted with the service token 
     // 2) A client certificate used to sign the message 

     // Instantiate a binding element that will require the username/password token in the message (encrypted with the server cert) 
     TransportSecurityBindingElement messageSecurity = SecurityBindingElement.CreateUserNameOverTransportBindingElement(); 

     // Create supporting token parameters for the client X509 certificate. 
     X509SecurityTokenParameters clientX509SupportingTokenParameters = new X509SecurityTokenParameters(); 
     // Specify that the supporting token is passed in message send by the client to the service 
     clientX509SupportingTokenParameters.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient; 
     // Turn off derived keys 
     clientX509SupportingTokenParameters.RequireDerivedKeys = false; 
     // Augment the binding element to require the client's X509 certificate as an endorsing token in the message 
     messageSecurity.EndpointSupportingTokenParameters.Endorsing.Add(clientX509SupportingTokenParameters); 

     // Create a CustomBinding based on the constructed security binding element. 
     return new CustomBinding(messageSecurity, httpsTransport); 

由該客戶端產生的SOAP消息非常接近達到我打電話的服務的要求,唯一的問題是,WSA:要地址已經被簽署,以及時間戳地址。

有沒有一種方法可以確切指定哪些WCF標頭被簽名?由於我需要限制客戶端只簽署TimeStamp標頭。

回答

0

通過自定義郵件頭,你可以這樣做:

//... rest of MessageContract 

[MessageHeader(ProtectionLevel = ProtectionLevel.Sign)] 
string MyCustomHeader; 

//... rest of MessageContract 

但我不相信,將與你的情況下工作,因爲你試圖註冊由您定義綁定插入SOAP頭。要修改這些頭文件,您可能需要實現IClientMessageInspector interface並將自定義行爲添加到客戶端配置以簽署TimeStamp頭。不知道你將如何訪問證書來執行簽名,但this may give you a good start.

+0

我不能使用ProtectionLevel設置,因爲我試圖簽署TimeStamp,它是以下鏈接中提到的基礎設施數據的內容http://msdn.microsoft .com/en-us/library/aa347692.aspx#Y1432。我該如何得到 – Edward 2011-05-09 21:10:54

+0

請查看答案中的IClientMessageInspector鏈接。它顯示瞭如何在發送到服務之前修改soap消息。您可以訪問BeforeSendRequest方法中的所有soap頭文件。你只需要弄清楚如何數字簽署wsa:To header項目並覆蓋肥皂信息中的現有項目(儘管這可能是困難的部分)。 – 2011-05-09 21:17:58

+1

我遇到的問題是WCF綁定同時簽署了wsa:To和wsu:TimeStamp,當我要求只簽署wsu:TimeStamp時。 我可以手動操作消息來替換現有的簽名,但我想盡量減少我們在SOAP消息上執行的自定義操作的數量。 我已經設法通過將messageVersion指定爲Soap11而不是Soap11WSAddressing10,然後手動添加WS-Addresing頭來避免需要手動實現簽名機制。 – Edward 2011-05-09 22:00:24