在ASP.Net Core 2.0中,我试图验证自定义中间件中的传入请求标头。
问题在于我不知道如何提取所有键值对标头。我需要的标题存储在受保护的属性
中protected Dictionary<string, stringValues> MaybeUnknown
到目前为止,我的中间件类看起来像这样:
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);
}
}
我的目标是提取所有请求标头,而不仅仅是一些我必须知道特定密钥的选定标头。
答案 0 :(得分:4)
httpContext.Request.Headers
是Dictionary
。您可以通过将标题名称作为键来返回标题的值:
context.Request.Headers["Connection"].ToString()