2010-03-25 82 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