3

I'm trying to set a header with a simple key/value-pair and want to read this from my API. So this is my call from client:

public async Task<T> Auth_GetAsync<T>(string path)
{
    var client = BaseHttpClient;
    var request = new HttpRequestMessage
    {
        RequestUri = new Uri(Path.Combine(client.BaseAddress.AbsoluteUri, path)),
        Method = HttpMethod.Get,
        Headers = { {"key", "param"} }
    };
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(RequestHeader));
    var task = await client.SendAsync(request);
    return task.IsSuccessStatusCode
                    ? JsonConvert.DeserializeObject<T>(await task.Content.ReadAsStringAsync())
                    : default(T);
}

when I'm trying to read the header in my action, I get it completely (with my key/value-pair)

public async Task<IEnumerable<string>> GetAsync()
{
    var i = Request.Headers;
    return await Task.Run(() => new[] { "value1", "value2" });
}

when I'm trying to do this with the ActionFilterAttribute and/or the IAuthorizationFilter my header always contains other keys, but never contains my key/value-pair. This is my Attribute:

public class RequiresKeyAttribute : ActionFilterAttribute, IAuthorizationFilter
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var req = filterContext.HttpContext.Request;
        var auth = req.Headers["key"]; // this is null here
    }

    public void OnAuthorization(AuthorizationContext filterContext)
    {
        var i = filterContext.HttpContext.Request.Headers;
    }
}

My target is, that the actionfilter checks whether the key is set in the header or not. I don't always want to check in my action if the key is set and validate it,...

Am I doing something wrong? Or is there solution to do this?

1 Answer 1

3

Maybe you inherited the wrong ActionFilterAttribute which comes from MVC, not Web Api because WebApi uses HttpActionContext, not ActionExecutingContext like below:

public override void OnActionExecuting(HttpActionContext actionContext)
{
    //code
}

You just use using System.Web.Http.Filters.ActionFilterAttribute from WebApi, it will be fine.

Sign up to request clarification or add additional context in comments.

4 Comments

nope, since I'm using the API with MVC 5. I'm using the FilterConfig which comes from System.Web.Mvc. Exception is: The given filter instance must implement one or more of the following filter interfaces: System.Web.Mvc.IAuthorizationFilter, System.Web.Mvc.IActionFilter, System.Web.Mvc.IResultFilter, System.Web.Mvc.IExceptionFilter, System.Web.Mvc.Filters.IAuthenticationFilter.
Everything should be from System.Web.Http, not System.Web.Mvc
I see... now it's working. But then, the template provided by Visual Studio for creating a WebApi is wrong... MVC-namespace was default
Thanks. it is also answer to this question stackoverflow.com/questions/71780279/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.