Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:36:50+00:00 2026-06-03T05:36:50+00:00

I’ve been looking into Authorization with AspNetWebApi and information is a little sparse on

  • 0

I’ve been looking into Authorization with AspNetWebApi and information is a little sparse on the subject.

I’ve got the following options:

  1. Pass API token on query string
  2. Pass API token as header
  3. Pass API token using Basic Auth
  4. Pass API token onto the request payload in json.

Which is generally the recommended method?

I’m also wondering for point 4), how would I go about inspecting the json payload in the OnAuthorization method on the AuthorizationFilterAttribute to check whether the API token is correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:36:51+00:00Added an answer on June 3, 2026 at 5:36 am

    If you want a truly secure option for authorization, something like OAuth is the way to go. This blog post provides a pretty thorough sample using the now obsolete WCF Web API but a lot of the code is salvageable. Or at least, go with using HTTP basic authentication as shown in this blog post. As Aliostad notes, make sure you’re using HTTPS if you go the Basic authentication route so the token stays secure.

    If you decide you want to roll your own (which almost always will be much less secure than either option above) then below is a code sample of what you’ll need for the AuthorizationHanlder if you go HTTP header route. Be aware there’s a good chance the way the UserPrinicipal is handled in Web API classes may change so this code is only good for the first preview release. You would need to wire-in the AuthorizationHandler like this:

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

    Code for header token:

    public class AuthenticationHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            var requestAuthTokenList = GetRequestAuthTokens(request);
            if (ValidAuthorization(requestAuthTokenList))
            {
                //TODO: implement a Prinicipal generator that works for you
                var principalHelper = GlobalConfiguration.Configuration
                    .ServiceResolver
                        .GetService(typeof(IPrincipalHelper)) as IPrincipalHelper;
    
                request.Properties[HttpPropertyKeys.UserPrincipalKey] = 
                    principalHelper.GetPrinicipal(request);
    
                return base.SendAsync(request, cancellationToken);
            }
            /*
            ** This will make the whole API protected by the API token.
            ** To only protect parts of the API then mark controllers/methods
            ** with the Authorize attribute and always return this:
            **
            ** return base.SendAsync(request, cancellationToken);
            */
            return Task<HttpResponseMessage>.Factory.StartNew(
                () => new HttpResponseMessage(HttpStatusCode.Unauthorized)
                    {
                        Content = new StringContent("Authorization failed")
                    });
        }
    
        private static bool ValidAuthorization(IEnumerable<string> requestAuthTokens)
        {
            //TODO: get your API from config or however makes sense for you
            var apiAuthorizationToken = "good token";
            var authorized = requestAuthTokens.Contains(apiAuthorizationToken);
    
            return authorized;
        }
    
        private static IEnumerable<string> GetRequestAuthTokens(HttpRequestMessage request)
        {
            IEnumerable<string> requestAuthTokens;
            if (!request.Headers.TryGetValues("SomeHeaderApiKey", out requestAuthTokens))
            {
                //Initialize list to contain a single not found token:
                requestAuthTokens = new[] {"No API token found"};
            }
            return requestAuthTokens;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.