Adding a bearer token for a custom request in the Web Api

I am using telogis third party library names in my project. For one of its features called Clustering, it is not possible to send a request header. Only the query string can be passed for clustering and all the API calling logic is done in the JS library.

My project uses bearer token authentication and is built with Web API 2. To solve this problem, I passed the access token in the query string and want to validate the request. I created below CustomAuthorize attribute for this:

public class ClusterRequestAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            base.OnAuthorization(actionContext);
        }

        public override Task OnAuthorizationAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            string accessToken = actionContext.Request.GetQueryNameValuePairs().Where(w => w.Key == "access_token").Select(w => w.Value).DefaultIfEmpty().FirstOrDefault();
            actionContext.Request.Headers.Remove("Authorization");
            actionContext.Request.Headers.Add("Authorization", accessToken);

            actionContext.ControllerContext.Request.Headers.Remove("Authorization");
            actionContext.ControllerContext.Request.Headers.Add("Authorization", accessToken);

            HttpContext.Current.Request.Headers.Remove("Authorization");
            HttpContext.Current.Request.Headers.Add("Authorization", accessToken);

            return base.OnAuthorizationAsync(actionContext, cancellationToken);
        }

        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            return base.IsAuthorized(actionContext);
        }
    }

      

But it IsAuthorized

always returns false. I have covered the internal Authorize API function using Git Link

According to this, I need to install actionContext.ControllerContext.RequestContext.Header

which is inaccessible due to the protection level as it is marked as internal

.

Is there any other work for this problem, or could it be better done?

+3
asp.net-mvc asp.net-web-api asp.net-identity


source to share


No one has answered this question yet

See similar questions:

thirty
Get IPrincipal from OAuth bearer token in OWIN

or similar:

1153
How do I get ASP.NET Web API to return JSON instead of XML using Chrome?
392
WCF vs ASP.NET Web API
372
How to secure ASP.NET Web API
4
Custom Authorized Attribute Called Too Many Times
3
Signalr Token Identification
0
Add claims to Asp.Net Web API 2 bearer token in ASP.net identity?
0
how to set header as oauth token request using authentication filter?
0
Using bearer authorization with PostMan
0
Allow all api control requests in demo mode
0
ASP.net API authenticates HTTP request using bearer token or other key-value header



All Articles
Loading...
X
Show
Funny
Dev
Pics