C# WebAPI.NETCore2请求检查请求';s标头不是空的
如何验证请求的页眉some header是否与book的bookId匹配C# WebAPI.NETCore2请求检查请求';s标头不是空的,c#,asp.net-web-api,asp.net-core-webapi,httpresponse,C#,Asp.net Web Api,Asp.net Core Webapi,Httpresponse,如何验证请求的页眉some header是否与book的bookId匹配 public IActionResult GetBooks() { // if 'some-header' value is empty, null , whitespace or request contains multiple ' some-header' headers then it should return UnauthorizedResult(); // if
public IActionResult GetBooks()
{
// if 'some-header' value is empty, null , whitespace or request contains multiple ' some-header' headers then it should return UnauthorizedResult();
// if 'some-header' is not above then it needs to be read from repository
}
public class Book
{
public string bookId {get; set;}
}
Request.Headers.GetValues()
将返回一个与HTTP请求中的头相对应的IEnumerable
,然后您可以验证它是否包含多个值,或者它是否仅一次检查是否为null或空白(包括空)
Request.Headers.GetValues()
将返回一个与HTTP请求中的头相对应的IEnumerable
,然后您可以验证它是否包含多个值,或者它是否仅一次检查是否为null或空白(包括空)
“IHeaderDictionary”不包含“GetValues”的定义,并且找不到接受“IHeaderDictionary”类型的第一个参数的可访问扩展方法“GetValues”(是否缺少using指令或程序集引用?)。我必须添加任何特定引用吗?@Lauren显然did方法不存在我用正确的方法更改了代码无法从“out System.Collections.Generic.IEnumerable”转换为“out Microsoft.Extensions.Primitives.StringValues”@Lauren检查我的新编辑,抱歉浪费了你一点时间,我应该先测试一下,然后再发布!应该是
返回新的UnauthorizedResult()代码>?“IHeaderDictionary”不包含“GetValues”的定义,并且找不到接受类型为“IHeaderDictionary”的第一个参数的可访问扩展方法“GetValues”(是否缺少using指令或程序集引用)?我正在使用.net core 2。我必须添加任何特定引用吗?@Lauren显然did方法不存在我用正确的方法更改了代码无法从“out System.Collections.Generic.IEnumerable”转换为“out Microsoft.Extensions.Primitives.StringValues”@Lauren检查我的新编辑,抱歉浪费了你一点时间,我应该先测试一下,然后再发布!应该是返回新的UnauthorizedResult()代码>?
Request.Headers.TryGetValue("some-header", out var headers);
if(headers.Count > 1 || string.IsNullOrWhiteSpace(headers.FirstOrDefault())){
return new UnauthorizedResult();
}