【发布时间】:2012-10-02 02:23:49
【问题描述】:
我有一个BasicAuthenticationAttribute 检查请求中的 Authorization 标头,但尽管它存在,它仍然认为 Authorization 标头为空:
public class BasicAuthenticationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Authorization == null)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
...
如果我检查 actionContext.Request.Headers 我可以看到 Authorization 列出:
{Connection: Keep-Alive
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-gb
Authorization: REDACTED_BUT_PRESENT==
Host: localhost:44300
Referer: https://localhost:44300/
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E)
}
更新
我刚刚检查了完整的请求标头,它们看起来像这样...我可以在第一部分中看到 Authorization 标头,但第二部分中的 Authorization 标头显然为空。
request.Headers
{Connection: Keep-Alive
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-gb
Authorization: REDACTED_BUT_PRESENT==
Host: localhost:1734
Referer: http://localhost:1734/
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E)
}
base {System.Net.Http.Headers.HttpHeaders}: {Connection: Keep-Alive
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-gb
Authorization: VXNlcjpQYXNzd29yZA==
Host: localhost:1734
Referer: http://localhost:1734/
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3; .NET4.0E)
}
Accept: {*/*}
AcceptCharset: {}
AcceptEncoding: {gzip, deflate}
AcceptLanguage: {en-gb}
Authorization: null
CacheControl: null
... removed for brevity ...
Warning: {}
【问题讨论】:
-
您是否在尝试基本身份验证。
-
@satish - 是的 - 所以我正在设置授权标头并使用我的操作过滤器对其进行检查。
-
我制作了一个消息处理程序,它似乎工作正常。我也会尝试使用动作过滤器。你能检查一下这是否有帮助gist.github.com/3872727 -Handler ,gist.github.com/3872715 - 单元测试
-
这对我来说基本上是一样的——我可以看到授权标头与发布的一样,但在消息处理程序中它认为
request.Headers.Authorization为空。让我检查单元测试生成的标头,看看它是否看起来不同。
标签: authorization asp.net-web-api