13

In ASP.Net Core 2.0, I am trying to validate the incoming request headers in a custom middleware.

The problem is that I don't how to extract all the key-value-pair headers. The headers that I need are stored in a protected property

protected Dictionary<string, stringValues> MaybeUnknown

My middleware class looks like this so far:

public class HeaderValidation
{
    private readonly RequestDelegate _next;
    public HeaderValidation(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext httpContext)
    {
        IHeaderDictionary headers = httpContext.Request.Headers; // at runtime headers are of type FrameRequestHeaders

        // How to get the key-value-pair headers?
        // "protected Dictionary<string, stringValues> MaybeUnknown" from headers is inaccessbile due to its protection level
        // Casting headers as Dictionary<string, StringValues> results in null

        await _next.Invoke(httpContext);
    }
}

My goal is to extract all request headers and not only a few selected headers for which I must know the specific keys.

philipp-fx
  • 619
  • 1
  • 8
  • 21
  • 1
    IHeaderDictionary implements IDictionary, you should be able to use all of the normal IDictionary APIs. – Tratcher Mar 15 '18 at 20:52
  • `var contentType = headers["Content-Type"];` – Jazzwave06 Mar 15 '18 at 21:01
  • 1
    Possible duplicate of [https://stackoverflow.com/questions/38794749/how-to-extract-custom-header-value?](https://stackoverflow.com/questions/38794749/how-to-extract-custom-header-value-in-web-api-message-handler-in-net-core) – Set Mar 16 '18 at 07:22
  • Thanks everyone for the great input! I think I was not clear enough with my question and rephrased it a little bit. The emphasis is on "all" headers and not only a few selected ones for which I must know the keys. – philipp-fx Mar 16 '18 at 16:48
  • 6
    Thanks to everyones input, I was able to get what I want. To get all the headers I needed to create a new Dictionary that takes the IHeaderDictionary as input, just like this: `var headerDictionary = new Dictionary(headers)` – philipp-fx Mar 16 '18 at 17:01
  • 4
    @philipp-fx - you should answer your own question here and accept the answer. This trick is really useful, and most definitely _not_ a duplicate of that other question :( - it's a shame the answer is muddle among the comments. – Steve Mar 06 '19 at 21:09

1 Answers1

14

httpContext.Request.Headers is a Dictionary. You can return the value of a header by passing the header name as the key:

context.Request.Headers["Connection"].ToString()
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 1
    Thanks for the answer Marc! Now I know how to extract headers for which I know the keys. This is already helpful, because I did not know how extract any key-value-pair header at all. Still, my goal is to extract all headers and I'd like to avoid the need to know all the keys. – philipp-fx Mar 16 '18 at 16:51
  • 3
    I was able to get what I want by creating a new dictionary that takes the `headers` variable as input. Thanks! – philipp-fx Mar 16 '18 at 17:03