BaxterBoom
BaxterBoom

Reputation: 43

WCF, Unable to extract httpheader when sending messages as <security mode="Message">

Im relativly new to WCF and/but "seem" to have got most things up and working.

I have the following IEndpointBehavior and IClientMessageInspector that I want to append when calling the service. It appends a token (HTTPHeader) that I want to check serverside (IIS)

public class AuthenticationTokenEndpointBehavior : IEndpointBehavior
{
    #region Member variables

    public class AuthenticationTokenMessageInspector : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            //throw new NotImplementedException();
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            string token = AuthenticationTokenManager.CreateToken();

            HttpRequestMessageProperty httpRequestMessage;
            object httpRequestMessageObject;
            if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
            {
                httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
            }
            else
            {
                httpRequestMessage = new HttpRequestMessageProperty();
                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
            }

            httpRequestMessage.Headers[AuthenticationTokenManager.AUTHENTICATION_TOKEN_NAME] = token;

            return null;
        }

    #endregion

    #region Methods

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        //throw new NotImplementedException();
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        AuthenticationTokenMessageInspector inspector = new AuthenticationTokenMessageInspector();
        clientRuntime.MessageInspectors.Add(inspector);
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        //throw new NotImplementedException();
    }

    public void Validate(ServiceEndpoint endpoint)
    {
        //throw new NotImplementedException();
    }

    #endregion
}

I have inherited and overriden the clientproxys CreateChannel, in which I append my IEndpointBehavior.

protected override ITheService CreateChannel()
    {
        this.Endpoint.Behaviors.Add(new AuthenticationTokenEndpointBehavior());
        return base.CreateChannel();
    }

This works very well for most of my bindings except when using <security mode="Message">. The headers are not sent to the server. Ive have googled abit but not found any information about this issue.

UPDATE 1: To clarify, the IClientMessageInspector.BeforeSendRequest IS called but no headers appears on the serverside.

UPDATE 2: I tried add a SoapHeader (MessageHeader) instead but no luck. Is there some sort of security "handshake" involved before the first request??

Upvotes: 2

Views: 731

Answers (2)

BaxterBoom
BaxterBoom

Reputation: 43

I solved this using IDispatchMessageInspector at the serviceend for WCF calls. Not the easy/quick way, with the HTTPModule validation, I was looking for. But it turned out ok. @Chris Dickson, thanks for your time!

Upvotes: 1

Chris Dickson
Chris Dickson

Reputation: 12135

Responding to your Update 2: depending on the exact configuration of your binding there may very well be a preliminary exchange of SOAP messages carrying WS-Trust security token request/response, before your application messages are exchanged. For example the default configuration of message security for the wsHttpBinding will do this.

Upvotes: 1

Related Questions