Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set http cookies (headers) in HTTP request
    primarykey
    data
    text
    <p>I'm trying to set up a WCF service hosted in IIS in ASP.Net compatibility mode that is protected via Forms Authentication and accessed via a .Net User Control in IE. (see <a href="https://stackoverflow.com/questions/4874774/secure-iis-hosted-wcf-service-for-access-via-ie-hosted-winforms-user-control">Secure IIS hosted WCF service for access via IE hosted user control</a>).</p> <p>The User Control in IE is needed because it uses a specific third-party control for which there doesn't exist anything comparable in Silverlight or AJAX.</p> <p>So I need the UserControl to set the authentication and session id cookies in the http request headers before it accesses the WCF service. My approach is to set up a Message inspector that does this.</p> <p>So I've defined the Message Inspector:</p> <pre><code>public class CookieInspector : IClientMessageInspector { public void AfterReceiveReply(ref Message reply, object correlationState) { } public object BeforeSendRequest( ref Message request, IClientChannel channel) { HttpRequestMessageProperty messageProperty; if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name)) { messageProperty = (HttpRequestMessageProperty) request.Properties[ HttpRequestMessageProperty.Name ]; } else { messageProperty = new HttpRequestMessageProperty(); request.Properties.Add( HttpRequestMessageProperty.Name, messageProperty ); } // Set test headers for now... messageProperty.Headers.Add(HttpRequestHeader.Cookie, "Bob=Great"); messageProperty.Headers.Add("x-chris", "Beard"); return null; } } </code></pre> <p>and an Endpoint behaviour:</p> <pre><code>public class CookieBehavior : IEndpointBehavior { public void AddBindingParameters( ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } public void ApplyClientBehavior( ServiceEndpoint endpoint, ClientRuntime clientRuntime) { clientRuntime.MessageInspectors.Add(new CookieInspector()); } public void ApplyDispatchBehavior( ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { } public void Validate(ServiceEndpoint endpoint) { } } </code></pre> <p>and I configure and create my channel and WCF client in code:</p> <pre><code>var ea = new EndpointAddress("http://.../MyService.svc"); // EDIT: Http cookies can't be set with WSHttpBinding :-( // var binding = WSHttpBinding(); var binding = new BasicHttpBinding(); // Disable automatically managed cookies (which enables user cookies) binding.AllowCookies = false; binding.MaxReceivedMessageSize = 5000000; binding.ReaderQuotas.MaxStringContentLength = 5000000; var cf = new ChannelFactory&lt;ITranslationServices&gt;(binding, ea); cf.Endpoint.Behaviors.Add(new CookieBehavior()); ITranslationServices service = cf.CreateChannel(); </code></pre> <p>However when I look at my request with Fiddler, the http header and cookie aren't set, and I have no clue why. I've read various articles on the Net, Stackoverflow etc that basically say that it should work, but it doesn't. Either I'm missing something obvious, or there's a bug in WCF or something else?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload