0

I'm working on an old (.net 4.7) Web api project. I don't care about authenticating the user as this is done upstream but i need to read the Authorization header to get some user details from a JWT. I haven't added any auth code etc so i was hoping i could just get the JWT from the Authorization header.

Basic code

var requestHeaders = Request.Headers;
requestHeaders.TryGetValues("Authorization", out IEnumerable<string> jwtToken);

Why is jwtToken null ?

Postman request enter image description here

Neil Gilbert
  • 125
  • 1
  • 9
  • Does this answer your question? [How can I retrieve Basic Authentication credentials from the header?](https://stackoverflow.com/questions/25855698/how-can-i-retrieve-basic-authentication-credentials-from-the-header) – Trevor Oct 21 '21 at 16:01
  • Add bearer with a space before the token – GH DevOps Oct 21 '21 at 16:08
  • it is strange that you try to convert to IEnumerable, I think it should be string or StringValues – Svyatoslav Ryumkin Oct 21 '21 at 19:15

1 Answers1

1

Issue was getting the headers from the request message instead of HttpContext.Current.Request.Headers

Neil Gilbert
  • 125
  • 1
  • 9
  • I had two webhooks with the same authentication code running. One worked, one found an empty parameter. Changing from Request.Headers.Authorization.Parameters to HttpContext.Current.Request.Headers["Authorization"] solved the issue. Thanks! – Alrekr May 06 '22 at 14:44