{"id":2133,"date":"2014-07-19T08:11:50","date_gmt":"2014-07-19T08:11:50","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2133"},"modified":"2014-07-19T08:11:50","modified_gmt":"2014-07-19T08:11:50","slug":"adding-data-to-wcf-headers","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/adding-data-to-wcf-headers\/","title":{"rendered":"Adding data to WCF headers"},"content":{"rendered":"<p>As I&#8217;ve covered this subject using WSE3, now to look at adding an SSO token to a WCF header.<\/p>\n<p><strong>Configuration<\/strong><\/p>\n<p>In your App.config you&#8217;ve probably got something like<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;system.serviceModel&gt;\r\n   &lt;client configSource=&quot;Config\\servicemodel-client-config.xml&quot; \/&gt;\r\n   &lt;bindings configSource=&quot;Config\\servicemodel-bindings-config.xml&quot; \/&gt;\r\n&lt;\/system.serviceModel&gt;\r\n<\/pre>\n<p>We&#8217;re going to add two more configuration files, as follows<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;system.serviceModel&gt;\r\n   &lt;client configSource=&quot;Config\\servicemodel-client-config.xml&quot; \/&gt;\r\n   &lt;bindings configSource=&quot;Config\\servicemodel-bindings-config.xml&quot; \/&gt;\r\n   &lt;!-- Additions --&gt;\r\n   &lt;behaviors configSource=&quot;Config\\servicemodel-behaviors-config.xml&quot; \/&gt;\r\n   &lt;extensions configSource=&quot;Config\\servicemodel-extensions-config.xml&quot; \/&gt;\r\n&lt;\/system.serviceModel&gt;\r\n<\/pre>\n<p>As the names suggest, these file will include the config for behaviors and extensions.<\/p>\n<p>Let&#8217;s take a look at the servicemodel-behaviors-config.xml first<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;\r\n&lt;behaviors&gt;\r\n   &lt;endpointBehaviors&gt;\r\n      &lt;behavior name=&quot;serviceEndpointBehaviour&quot;&gt;\r\n         &lt;ssoEndpointBehaviorExtension \/&gt;\r\n         &lt;!-- insert any other behavior extensions here --&gt;\r\n      &lt;\/behavior&gt;\r\n   &lt;\/endpointBehaviors&gt;\r\n&lt;\/behaviors&gt;\r\n<\/pre>\n<p>Now we need to actually define what implements <em>ssoEndpointBehaviorExtension<\/em>. So in the servicemodel-extensions-config.xml configuration, we might have something like the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;\r\n&lt;extensions&gt;\r\n   &lt;behaviorExtensions&gt;\r\n      &lt;add name=&quot;ssoEndpointBehaviorExtension&quot;\r\n            type=&quot;SsoService.SsoEndpointBehaviorExtensionElement, SsoService&quot;\/&gt;\r\n      &lt;!-- insert other extensions here --&gt;\r\n   &lt;\/behaviorExtensions&gt;\r\n&lt;\/extensions&gt;\r\n<\/pre>\n<p>So as we can see, the <em>ssoEndpointBehaviorExtension<\/em> behavior is associated with the SsoService assembly and the type SsoEndpointBehaviorExtensionElement.<\/p>\n<p><strong>Implementing the behavior\/extension<\/strong><\/p>\n<p>Unlike WSE3 we do not need to use an attribute to associate the extension with a service call.<\/p>\n<p>Let&#8217;s start by looking at the SsoEndpointBehaviorExtensionElement behavior extension.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class SsoEndpointBehaviorExtensionElement : BehaviorExtensionElement\r\n{\r\n   public override Type BehaviorType\r\n   {\r\n      get { return typeof(SsoEndpointBehavior); }\r\n   }\r\n   \r\n   protected override object CreateBehavior()\r\n   {\r\n      return new SsoEndpointBehavior();\r\n   }\r\n}\r\n<\/pre>\n<p>The code above relates to the actual extension element, so really just creates the actual behavior when required. Here&#8217;s the SsoEndpointBehavior.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class SsoEndpointBehavior : IEndpointBehavior\r\n{\r\n   public void AddBindingParameters(ServiceEndpoint endpoint, \r\n                 BindingParameterCollection bindingParameters)\r\n   {\r\n   }\r\n\r\n   public void ApplyClientBehavior(ServiceEndpoint endpoint, \r\n                 ClientRuntime clientRuntime)\r\n   {\r\n      clientRuntime.MessageInspectors.Add(new SsoMessageInspector());\r\n   }\r\n\r\n   public void ApplyDispatchBehavior(ServiceEndpoint endpoint, \r\n                 EndpointDispatcher endpointDispatcher)\r\n   {\r\n   }\r\n\r\n   public void Validate(ServiceEndpoint endpoint)\r\n   {\r\n   }\r\n}\r\n<\/pre>\n<p>This code simply adds the inspector to the message inspector. Finally let&#8217;s look at the code that actual intercepts the send requests to add the SSO token to the header.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class SsoMessageInspector : IClientMessageInspector\r\n{\r\n   public object BeforeSendRequest(ref Message request, IClientChannel channel)\r\n   {\r\n      request.Headers.Add(MessageHeader.CreateHeader(&quot;ssoToken&quot;, \r\n              String.Empty, \r\n              SsoManager.TokenString);\r\n      return null;\r\n   }\r\n\r\n   public void AfterReceiveReply(ref Message reply, object correlationState)\r\n   {\r\n   }\r\n}\r\n<\/pre>\n<p>In the above code, we create an addition to the message header, with the name &#8220;ssoToken&#8221; followed by any namespace and then the value we wish to store with the header item. In this case our SSO token.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I&#8217;ve covered this subject using WSE3, now to look at adding an SSO token to a WCF header. Configuration In your App.config you&#8217;ve probably got something like &lt;system.serviceModel&gt; &lt;client configSource=&quot;Config\\servicemodel-client-config.xml&quot; \/&gt; &lt;bindings configSource=&quot;Config\\servicemodel-bindings-config.xml&quot; \/&gt; &lt;\/system.serviceModel&gt; We&#8217;re going to add two more configuration files, as follows &lt;system.serviceModel&gt; &lt;client configSource=&quot;Config\\servicemodel-client-config.xml&quot; \/&gt; &lt;bindings configSource=&quot;Config\\servicemodel-bindings-config.xml&quot; \/&gt; &lt;!&#8211; Additions [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[77,11],"tags":[],"class_list":["post-2133","post","type-post","status-publish","format-standard","hentry","category-soap","category-wcf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2133"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2133\/revisions"}],"predecessor-version":[{"id":2154,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2133\/revisions\/2154"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}