2

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 Secure IIS hosted WCF service for access via IE hosted user control).

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.

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.

So I've defined the Message Inspector:

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;
    }
}

and an Endpoint behaviour:

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) {
    }
}

and I configure and create my channel and WCF client in 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<ITranslationServices>(binding, ea);
cf.Endpoint.Behaviors.Add(new CookieBehavior());    

ITranslationServices service = cf.CreateChannel();

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?

Community
  • 1
  • 1
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Oct 26 '12 at 05:26

2 Answers2

3

Well I figured it out, if I use a basicHttpBinding instead of a WSHttpBinding it works. No idea why though...

  • Did you ever find out why this didn't work with WSHttpBinding? I'm running into the same problem, but it works with basicHttpBinding – Peder Rice Aug 23 '11 at 20:51
  • No sorry finally we dropped ASP.NET completely for the project and went with a WinForms + WCF + ClickOnce architecture and the point became moot. – Christophe Keller Apr 05 '12 at 11:15
0

WSHttpBinding may be composed of more than one physical message to one logical message. So when successive physical calls are made, they may not be carrying the cookie appropriately

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
WLY
  • 1