0

So I my app chanced authentication Management, previously all headers were added in a load balancer. The new method works and in order to not have to rewrite code we are using an angular interceptor to inject the headers in the HTTP request there

HttpContext.Current.Response.AddHeader("ICAM_RIOT", "RIOT");

 private void hasResponseHeaderCollection(string headerName, string Value)
    {
      if (!HttpContext.Current.Response.Headers.AllKeys.Any(k => string.Equals(k, headerName)))
      {
        HttpContext.Current.Response.AddHeader(headerName, Value);
      }
    }
    private string GetHeaderStringValue(string headerName)
    {
      return HttpContext.Current.Request.Headers[headerName] ?? "";
    }
    

Please see the photos,

All the headers are UPPER CASE

WHEN received on the .Net api side they are camel Case

For instance ICAM_MAIL becomes Icam_mail

I even tried on the .net side to add a rseponse header all caps and it comes back camel case as well

Client Side Headers

response headers

  • Http headers are [not case sensitive](https://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive/5259004), so why does it matter? – gunr2171 Jan 07 '22 at 21:21
  • 1
    I second the comment by @gunr2171 . You should process both on the client and on the server side ignoring casing. – Alberto Chiesa Jan 07 '22 at 21:23
  • .Net net side is not fiding the header then I pass in a string looking for it for example it cannot find a head for "ICAM_MAIL" but can find "Icam_mail" – China Syndrome Jan 07 '22 at 21:33

1 Answers1

2

HTTP headers are case insensitive. Whenever you lookup a header value by Key it needs to be a case insensitive match.

Request.Headers[headerName] handles this for you, but Response.Headers.AllKeys.Any(...) will not because you're doing your own string comparison, which defaults to case sensitive matching. Use the overload of string.Equals that allows for a StringComparison object and use StringComparer.InvariantCultureIgnoreCase.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • You are correct, I guess before the load balancer always injected them in uppercase, and Angular does something different, that one overload did the trick – China Syndrome Jan 07 '22 at 22:29