Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web Api: How to pass an access token (oAuth 2.0) using URL parameter?

Tags:

Do you have any idea how I can use, an access_token generated by the default asp.net web api 2 OAuth 2 authorization mechanism, in the url parameters. Currently I am able to authorize successfully by sending a request with Authorization header like this:

Accept: application/json
Content-Type: application/json
Authorization: Bearer pADKsjwMv927u...

What I want is to enable the authorization through URL parameter like this:

https://www.domain.com/api/MyController?access_token=pADKsjwMv927u...
like image 637
mynkow Avatar asked Jan 31 '14 16:01

mynkow


People also ask

Is it OK to pass access token in URL?

"Don't pass bearer tokens in page URLs: Bearer tokens SHOULD NOT be passed in page URLs (for example as query string parameters). Instead, bearer tokens SHOULD be passed in HTTP message headers or message bodies for which confidentiality measures are taken."

How do I send token to Web API?

Use Webapi 2 Parameter binding. http://localhost:29001/api/test/Z4TTHmY98=gFw2rG/ [HttpGet] [Route("/test/{AuthToken}") public IHttpActionResult Test([FromUri] string AuthToken) { //Do whatever you want to do. } Careful with the route. It needs to be proper and configured in webapiconfig .

How do I call API access token?

You need to perform the following: Register your app in the Security Token Service, based on IdentityServer3. Within your app, acquire an access token from the STS. Add an authorization header Bearer access_token and call the Sitefinity Web API.


2 Answers

Well - I agree that the header is a much better alternative - but there are of course situations where the query string is needed. The OAuth2 spec defines it as well.

Anyways - this feature is built into the Katana OAuth2 middleware:

http://leastprivilege.com/2013/10/31/retrieving-bearer-tokens-from-alternative-locations-in-katanaowin/

public class QueryStringOAuthBearerProvider : OAuthBearerAuthenticationProvider {     readonly string _name;      public QueryStringOAuthBearerProvider(string name)     {         _name = name;     }      public override Task RequestToken(OAuthRequestTokenContext context)     {         var value = context.Request.Query.Get(_name);          if (!string.IsNullOrEmpty(value))         {             context.Token = value;         }          return Task.FromResult<object>(null);     } } 

And then:

var options = new JwtBearerAuthenticationOptions {     AllowedAudiences = new[] { audience },     IssuerSecurityTokenProviders = new[]         {             new SymmetricKeyIssuerSecurityTokenProvider(                 issuer,                 signingKey)         },     Provider = new QueryStringOAuthBearerProvider(“access_token”) }; 
like image 158
leastprivilege Avatar answered Oct 07 '22 12:10

leastprivilege


So, go to Global.asax and add this method:

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

UPDATE: Check out @leastprivilege answer. Much better solution.

like image 21
mynkow Avatar answered Oct 07 '22 11:10

mynkow