How do you obtain a strongly typed header class from the namespace System.Net.Http.Headers
from an ASP.NET Core controller? In a controller derived from Controller
, Request.Headers
is available, but it just returns IHeaderDictionary
. There is also an extension method HeaderDictionaryTypeExtensions.GetTypedHeaders
, but it returns RequestHeaders
, which has only certain headers. The class HttpRequestHeaders
has the most comprehensive list of headers, but it's not clear how to access it.
For example, how would you get a AuthenticationHeaderValue
? One option is AuthenticationHeaderValue.Parse(Request.Headers["Authorization"])
, but that requires hard coding the header name. Perhaps there is a non-hard-coded way to get to HttpRequestHeaders.Authorization
.
Use AuthenticationHeaderValue
to parse the header string into an object with Scheme
and Parameter
properties.
var auth = AuthenticationHeaderValue.Parse(Request.Headers[HeaderNames.Authorization]);
if (auth.Scheme != expectedScheme || !MyVerifyAuthParamteter(auth.Parameter)) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With