How to set up Web API to allow Acces Token in the URL as a parameter with DotnetOpenAuth in WEB API 2

1.2k views Asked by At

We have a file export module in our application, and we want to use it in the client application which uses the web api. But I can't download files with Ajax requests.

I would like to use a get query which will contains the Bearer Token as a query parameter.

Is this possible to set up in the WEB API?

1

There are 1 answers

0
Lóri Nóda On BEST ANSWER

In the meantime I found out the response for my question, in global asax we can have the following function which will do the work for us:

    void Application_BeginRequest(object sender, EventArgs e)
    {
        if (ReferenceEquals(null, HttpContext.Current.Request.Headers["Authorization"]))
        {
            var token = HttpContext.Current.Request.Params["Authorization"];
            if (!String.IsNullOrEmpty(token))
            {
                HttpContext.Current.Request.Headers.Add("Authorization", "Bearer " + token);
            }
        }
    }

There is only one problem with the URL thing approach, we need to add the extra Authorization parameter to the action we want to invoke because we can't remove the parameter from the "Params" collection because it's read only.