Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the post form parameter values at an Action filter in web api,C#?

How can I read the post form parameter values using the Request in web api? I have a controller

    [Route("")]
    [HttpPost]
    [AuthenticationAttribute]
    public void PostQuery()
    {
          //some code
    }

I have defined AuthenticationAttribute class seperately

 public class AuthenticationAttribute : Attribute, IAuthenticationFilter
{


    public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
    {
    // I want to read the post paramter values over here
    }

    public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
    {
        return Task.Run(
            () =>
                {

                });
    }

    public AuthenticationAttribute()
    {

    }
}

I want to check the post parameter in the AuthenticateAsync function.

I tried doing

context.Request.Content.ReadAsStringAsync().Result;

but this string is empty. I am able to read the query parameters using

context.Request.GetQueryNameValuePairs();

But could not find a way to get the post form parameters. Any help is appreciated.

like image 377
sachin Avatar asked Feb 14 '17 21:02

sachin


1 Answers

var reader = new StreamReader(HttpContext.Current.Request.InputStream);
var content = reader.ReadToEnd();
var jObj = (JObject)JsonConvert.DeserializeObject(content);

foreach (JToken token in jObj.Children())
{
    if (token is JProperty)
    {
        var prop = token as JProperty;

        if (prop.Name.Equals("skipExternal") && prop.Value.ToString().Equals("True"))
        {
            // Logic...
        }
    }
}

This is the code I used. I wanted to check weather the parameter skipExternal was sent as True in post parameters.

like image 106
Yedidya Schwartz Avatar answered Oct 09 '22 11:10

Yedidya Schwartz