title: Sending Cookies over WCF using the ChannelFactory
body: <p>I use an IOC container which provides me with IService.
  In the case where IService is a WCF service it is provided by a channel factory
  When IService lives on the same machine it is able to access the same cookies and so no problem however once a WCF Service is called it needs to be sent those cookies.</p>
  
  <p>I've spent a lot of time trying to find a way to send cookies using a channel factory and the only way I could find that works is the following</p>
  
  <pre><code>var cookie = _authenticationClient.GetHttpCookie();
  HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
  httpRequestProperty.Headers.Add(HttpRequestHeader.Cookie, cookie.Name + "=" + cookie.Value);
  using(var scope = new OperationContextScope((IClientChannel)_service))
  {
     OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
     var result = _service.Add(details);
     if (result.Result == RPGResult.Success)
     {
         return RedirectToAction("Index", "Home", result.Id);
     }
  }
  </code></pre>
  
  <p>The problem with me using that method is that I have to know that I'm calling a WCF Service which is not always the case. I've tried writing a wrapper for the ChannelFactory that opens a new operationcontextscope when it creates a new service and various other solutions but nothing has worked.</p>
  
  <p>Anyone have any experience with sending cookies over WCF Services?</p>
  
  <p>I found a solution involving using SilverLight, unfortunately I'm not using silverlight, the solution is here: <a href="http://greenicicleblog.com/2009/10/27/using-the-silverlight-httpclient-in-wcf-and-still-passing-cookies/" rel="nofollow">http://greenicicleblog.com/2009/10/27/using-the-silverlight-httpclient-in-wcf-and-still-passing-cookies/</a>
  Unfortunately standard .net doesn't contain the IHttpCookieContainerManager interface</p>
  
  <p>Ideally I would be able to use something similar,i.e. I would be able to tell the channelfactory to pass a cookie whenever it opened.</p>
  
  <p>If anyone has a better way to pass a token that is used for authentication that would be appreciated too.</p>