官术网_书友最值得收藏!

Defining a CustomBinding without a timestamp header

For WCF bindings that use message-layer security, a timestamp header will be added in the SOAP envelope to ensure the timely delivery of the message so as to prevent a potential message-replaying attach. However, some non-WCF service platforms may not expose this header. When working with this kind of service client or service, we will need to prevent the WCF message engine from generating the timestamp header.

How to do it...

Using WSHttpBinding as an example, we can create a customized binding that derives most of the setting of the built-in WSHttpBinding (but suppresses the timestamp header generation).

The following code snippet demonstrates how to create the CustomBinding and configure the certain binding element to disable timestamp header generation.

private static Binding GetCustomHttpBinding()
{
   WSHttpBinding wshttp = new WSHttpBinding();
   var bec = wshttp.CreateBindingElements();

   SecurityBindingElement secbe = bec.Find<SecurityBindingElement>();
   // Not to include Timestamp header
 secbe.IncludeTimestamp = false;
   // Suppress the message relay detection
 secbe.LocalServiceSettings.DetectReplays = false;
 secbe.LocalClientSettings.DetectReplays = false;

   CustomBinding cb = new CustomBinding(bec);
   return cb;
}

The code first locates the SecurityBindingElement instance from the default element collection of wsHttpBinding. It then sets the IncludeTimestamp property to false. Also, it is necessary to turn off the DetectReplays property on the LocalServiceSettings and LocalClientSettings members.

Finally, we can apply this CustomBinding to any endpoint that needs to suppress the timestamp header.

How it works...

Since the timestamp header is a security feature that performs a message-replaying check, the WCF programming model exposes this setting through the SecurityBindingElement type. However, only setting the SecurityBindingElement.IncludeTimestamp to false is not enough, because this only helps remove the timestamp header; the runtime will still perform replay detection on incoming/outgoing messages. Therefore, we also need to turn off the DetectReplays property on LocalServiceSettings and LocalClientSettings collection.

By comparing the underlying SOAP messages, we can find the obvious difference in the SoapHeader section before and after we disable the timestamp header generation. The next screenshot is a SOAP message captured before removing the timestamp header:

How it works...

The following screenshot is for a SOAP message captured after removing the timestamp header:

How it works...

See also

  • Complete source code for this recipe can be found in the \Chapter 2\recipe6\ folder
主站蜘蛛池模板: 神木县| 阳高县| 中牟县| 响水县| 龙门县| 江川县| 长海县| 福州市| 台北市| 嘉善县| 会同县| 彭州市| 若羌县| 大名县| 靖安县| 林甸县| 和龙市| 张家口市| 临汾市| 扎兰屯市| 封开县| 湘潭县| 绥化市| 枣强县| 福海县| 北海市| 惠来县| 镇原县| 淮阳县| 昌宁县| 义乌市| 梧州市| 汉寿县| 邢台县| 常熟市| 同心县| 鹤岗市| 宁远县| 淳化县| 梅河口市| 上犹县|