我有一个BasicAuthenticationAttribute检查请求中的Authorization标头但是尽管它存在,它仍然认为Authorization标头为null:
public class BasicAuthenticationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Authorization == null)
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
...
Run Code Online (Sandbox Code Playgroud)
如果我检查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)
}
Run Code Online (Sandbox Code Playgroud)
更新
我刚刚检查了完整的请求标头,它们看起来像这样......我可以看到第一部分中的Authorization标头,但第二部分中的Authorization标头显然为null.
request.Headers
{Connection: Keep-Alive
Accept: …Run Code Online (Sandbox Code Playgroud) 在我的控制器中,我试图调用我的API,但无法传递Autherization令牌。
我创建了一个配置变量并在其中传递令牌,但得到以下响应:
900902缺少凭据未提供所需的OAuth凭据。确保您的API调用调用具有标头:“授权:承载ACCESS_TOKEN”
** JS代码**
app.controller("AuthenticationController", function($scope, API_URL, vcRecaptchaService, $http) {
var config = {
headers: {
'Authorization': 'Bearer 00000-e5673-346756f-8676-f7567561a'
}
};
$scope.verifyRecaptcha = function() {
if (vcRecaptchaService.getResponse() === "") {
alert("User did not resolve the recaptcha")
} else {
var post_data = {
'g-recaptcha-response': vcRecaptchaService.getResponse()
}
$http.post('https:1.1.1.1/abc/vdc/verify', config, post_data).success(function(response) {
if (response.success === true) {
alert("Successfully resolved the recaptcha.");
} else {
alert("User verification failed");
}
})
.error(function(error) {
alert("Error Occured while resolving recaptcha.");
console.log(error);
})
}
}
Run Code Online (Sandbox Code Playgroud)