JSONP callback not being recognized with message handler

Topics: Web Api
Feb 1, 2012 at 4:38 PM

I made a message handler to handle my user token authentication. Similar to the facebook example.

When the url ends in /authtoken, I attempt to verify the credentials supplied in the query string.

I build a JsonObject of the response and pass it back.

It returns as JSON fine. But the built in handler for when callback is in the URL does not fire, and it does not wrap it in the method.

I've been struggling with making a custom method to wrap the response before sending it back, but I believe this functionality should be handled, and I might be missing something. This definitely sounds like an issue to me though.

[AuthenticationConfiguration]
    public class UserTokenAuthenticationMessageHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.RequestUri.Segments.Last() == "authtoken")
            {
                return Task.Factory.StartNew(() =>
                {
                    string userName = request.QueryString("username");
                    string password = request.QueryString("password");

                    var response = new HttpResponseMessage();
                    response.RequestMessage = request;

                    string token;
                    ObjectContent contentObject;
                    if (!UserTokenService.Instance.ValidateUser(userName, password, out token))
                    {
                        contentObject = response.AsUnauthorizedJsonContent();
                    }
                    else
                    {
                        var json = new JsonObject()
                        {
                            { "authToken", token },
                            { "expiresOn", "never" }
                        };
                        contentObject = new ObjectContent(json.GetType(), json);
                    }

                    response.SetJsonContent(contentObject);

                    return response;
                });
            }

            return base.SendAsync(request, cancellationToken);
        }
    }

        public static void SetJsonContent(this HttpResponseMessage response, ObjectContent objectContent)
        {
            MediaTypeHeaderValue mediaType;
            if (!string.IsNullOrEmpty(response.RequestMessage.QueryString("callback")))
            {
                //content = new StringContent(String.Format("{0}({1})", response.RequestMessage.QueryString("callback"), json));
                mediaType = new MediaTypeHeaderValue("text/javascript");
                response.StatusCode = HttpStatusCode.OK;
            }
            else
            {
                mediaType = new MediaTypeHeaderValue("application/json");
            }

            response.Content = objectContent;
            response.Content.Headers.ContentType = mediaType;
            response.Headers.CacheControl = new CacheControlHeaderValue() { NoCache = true };
        }

Feb 2, 2012 at 8:20 PM

Bump? anyone have any input on this? It's really started to anger me severely.

I'm almost to the point where I'm just going to write a method that will do do manual checks on all the accept types I support and do all the formatting thing manually, defeating some the purpose of using WebApi.