2010-03-25 84 views
1

我需要使用輪詢技術來通知客戶端有關服務器端的更改。所以我試圖使用DuplexHttpBinding(http://code.msdn.microsoft.com/duplexhttp)。我對非安全消息工作正常,但我需要在我的項目(UsernameForCertificate)中使用消息級安全性。好吧,我決定添加SymmetricSecurityBindingElement綁定集合:WCF自定義消息安全

var securityElement = SecurityBindingElement.CreateUserNameForCertificateBindingElement(); 
collection.Add(securityElement); 

然後問題發生了。如果我們使用消息級安全的所有信息包括安全報頭與消息簽名,這樣的:

<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:s="http://www.w3.org/2003/05/soap-envelope"> 
.... 
</o:Security> 

和自定義它們通過自定義的請求信道發送沒有安全頭輪詢消息,因此在發送這個消息出現異常通過與消息級安全通道:

System.ServiceModel.Security.MessageSecurityException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 
No signature message parts were specified for messages with the 'http://samples.microsoft.com/duplexhttp/pollingAction' action. 

請諮詢解決辦法如何把他們自定義的請求通道內之前適當的安全頭添加到我的自定義輪詢消息。您可以通過之前發佈的鏈接下載源代碼,只需使用UsernameForCertificate安全性來重現問題即可。 謝謝。

回答

2

經過幾天的深入調查&我找到了解決方案。看起來我們應該在創建自定義Channel Facroty & Channel Listener時修改ChannelProtectionRequirements以將加密&簽名部分添加到我們的自定義消息中。這裏是示例:

private static void ApplyChannelProtectionRequirements(BindingContext context) 
    { 
     var cpr = context.BindingParameters.Find<ChannelProtectionRequirements>(); 
     if (cpr != null) 
     { 
      XmlQualifiedName qName = new XmlQualifiedName("customHeader", "namespace"); 
      MessagePartSpecification part = new MessagePartSpecification(qName); 
      cpr.IncomingEncryptionParts.AddParts(part, "incomingAction"); 
      cpr.IncomingSignatureParts.AddParts(part, "incomingAction"); 
      cpr.OutgoingEncryptionParts.AddParts(part, "outgoingAction"); 
      cpr.OutgoingSignatureParts.AddParts(part, "outgoingAction"); 
     } 
    } 
+0

你在哪裏調用你的私有方法'ApplyChannelProtectionRequirements' ?. – 2017-11-19 17:39:33