Get a strongly typed header class in the ASP.NET core

How to get a strongly typed header class from the System.Net.Http.Headers namespace from an ASP.NET kernel controller? Request.Headers is available in the controller received from the Controller , but it simply returns an IHeaderDictionary . There is also the HeaderDictionaryTypeExtensions.GetTypedHeaders extension HeaderDictionaryTypeExtensions.GetTypedHeaders , but it returns RequestHeaders , which only has specific headers. The HttpRequestHeaders has the most comprehensive list of headers, but it is unclear how to access it.

For example, how would you get an AuthenticationHeaderValue ? One parameter is AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]) , but this requires strong encoding of the header name. Perhaps there is a hard-coded way to get to HttpRequestHeaders.Authorization .

+13
asp.net-core asp.net-controls
source share
1 answer

Use AuthenticationHeaderValue to parse the title bar into an object with the Scheme and Parameter properties.

 var auth = AuthenticationHeaderValue.Parse(Request.Headers[HeaderNames.Authorization]); if (auth.Scheme != expectedScheme || !MyVerifyAuthParamteter(auth.Parameter)) ... 
+12
source share

All Articles