2

I am having a tough time figuring this out, but when I use a custom message handler in my Web API (created in VS2013) none of the /token request get processed through my handler.

What I'm trying to do is assist our support crew by implementing some logging to save request / response values for a few days. This will allow them to see the request and responses as raw as possible.

It's working, for everything except "/token" requests. We need to process the requests and responses for "/token" and "/authenticate" as a large percentage of our support calls end up being username and password issues.

I also need to do this in a message handler so I can isolate the code to message handlers.

Here is a sample handler I'm testing with in an isolated project. It's only in place ATM to debug/test this issue. I've also implemented a DelegatingHandler as well with the same results.

public class MyMessageProcessingHandler : MessageProcessingHandler {
    protected override HttpRequestMessage ProcessRequest(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {
        Trace.WriteLine(string.Format("{0} {1}", request.Method, request.RequestUri));

        return request;
    }

    protected override HttpResponseMessage ProcessResponse(HttpResponseMessage response, System.Threading.CancellationToken cancellationToken) {
        Trace.WriteLine("response!");

        return response;
    }
}

in WebApiConfig.Register method I add the message handler to the config's message handler collection. (I also tried Global.asax.cs is the Application_Start method)

GlobalConfiguration.Configuration.MessageHandlers.Add(new MyMessageProcessingHandler());

The order doesn't seem to matter - I've tried it as the first line of code, or the last.I've tried to Insert after the passive message handler is added by

config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new MyMessageProcessingHandler());

Whenever I make an api/Values request or any api/Controller request the custom message handler handles the request just fine. However, when I post (or get) to /token (yes-properly) the custom message handler doesn't process the request.

I would like to know how to use a Message Handler to process the /token & /authenticate requests. I appreciate all your help!

Thanks,

-Rick

1 Answer 1

1

I went with a custom IHttpModule. It ended doing what I wanted in the way I wanted it by giving me direct access to the requests and allowing me to inspect them; even the authentication requests.

Thanks to all that looked at my issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.