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

  • SEARCH
  • Home
  • 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 8580343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:48:32+00:00 2026-06-11T20:48:32+00:00

I created an ActionFilterAttribute for my web api in order to authorize people. Getting

  • 0

I created an ActionFilterAttribute for my web api in order to authorize people. Getting accessToken by RequestUri is okay, however i want to send it in form data. While reading Request.Content in onActionExecuting method of ActionFilterAttribute, server always has an empty result. How can i solve this problem? The code is as like as below:

    public class RequireAuthorization : ActionFilterAttribute
{

    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        actionContext.Request.Content.ReadAsStringAsync().ContinueWith((t) =>
        {
            try
            {
                //query will result in empty string
                string query = t.Result;

                string UserID = HttpUtility.ParseQueryString(query).Get("UserID");
                string accessToken = HttpUtility.ParseQueryString(query).Get("AccessToken");

                UserRepository repository = new UserRepository();
                repository.IsTokenValid(Convert.ToInt32(UserID), accessToken);
            }
            catch (Exception ex)
            {
                var response = new HttpResponseMessage
                {
                    Content =
                        new StringContent("This token is not valid, please refresh token or obtain valid token!"),
                    StatusCode = HttpStatusCode.Unauthorized
                };

                throw new HttpResponseException(response);
            }
        });


        base.OnActionExecuting(actionContext);
    }
}
  • 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-11T20:48:33+00:00Added an answer on June 11, 2026 at 8:48 pm

    It’s is because the HttpContent has been read by the formatter before ActionFilter. Web API only allows reading content once. So you are unable to read it again.

    Here is a possible solution to you. First, make your action parameter as FormDataCollection:

        [RequireAuthorization]
        public HttpResponseMessage PostTodo(FormDataCollection formData)
        {
            Todo todo = formData.ReadAs<Todo>();
            // ...
    

    Then, get it in ActionFilter by code:

        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            var formData = actionContext.ActionArguments["formData"] as FormDataCollection;
            if (formData != null)
            {
                var userID = formData.Get("UserID");
                var accessToken = formData.Get("AccessToken");
                // authorize
            }
    
            base.OnActionExecuting(actionContext);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a Web Api filter (using System.Web.Http.Filters.ActionFilterAttribute ) but I am unable to
I have created a filter which inherits the System.Web.Http.Filters.ActionFilterAttribute in the asp.net web api
I created a keyspace with the cassandra-cli tool, and i can use it. However
created a new IIS7 web site with Intergrated .net 4.0 app pool. URLs ending
I need to audit log calls to my Web API, ideally I'd like to
Created an array that needs randomizing however the array_rand is not working. Is my
I refactored an attribute, which implements Basic Http Authentication in the Web api, to
Created a button, now tried to add it to the screen and im getting
i created the following ActionFilterAttribute to check if a user is granted access to
Created a views.py method to get the data posted from an html form. As

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.