简体   繁体   English

访问Azure中部署的Web API时,“对此请求的授权被拒绝”

[英]“Authorization has been denied for this request” when accessing Web API deployed in Azure

Following on from this question: AADSTS50013: Assertion audience claim does not match the required value 接下来是这个问题: AADSTS50013:断言受众声明与所需值不匹配

I've now successfully got the web apps running with this security model: SPA application using adal.js/adal_angular.js to authenticate via AAD. 现在,我已经成功使用以下安全模型运行Web应用程序:使用adal.js / adal_angular.js通过AAD进行身份验证的SPA应用程序。 Returned token is passed to web api [API1] that runs on the same machine. 返回的令牌将传递到在同一台计算机上运行的Web API [API1]。 That web api gets a new token on behalf of the user to access a downstream API [API2]. 该Web api代表用户获取一个新令牌,以访问下游API [API2]。

The downstream api gets a new token on behalf of the user to access another downstream API [API3]. 下游api代表用户获取新令牌,以访问另一个下游API [API3]。

Now, when I have [API2] running locally, this is all working. 现在,当我在本地运行[API2]时,一切正常。
However, when I deploy that web app to my Azure subscription, and attempt to call it (without changing anything else other than the url in the REST API call from [API1]), I get the following: 但是,当我将该Web应用程序部署到我的Azure订阅中并尝试调用它时(除了更改[API1]的REST API调用中的URL以外,没有其他任何更改),我得到以下信息:

{"Message":"Authorization has been denied for this request."}

There doesn't appear to be any other error details returned or in the Fiddler trace. Fiddler跟踪中似乎没有返回任何其他错误详细信息。 Comparing the jwt token payloads between the call that works and the one that doesn't, doesn't reveal much. 在有效的调用与无效的调用之间比较jwt令牌有效载荷,并没有太多揭示。 They appear the same other than the expiry claims and the "aio" (not sure what that is). 除了到期声明和“ aio”(不确定是什么)外,它们看起来是相同的。

The only change is the URL of the deployed web app (from http://localhost:8080/ to http://mywebappname.azurewebsites.net/ ) 唯一的更改是已部署的Web应用程序的URL(从http://localhost:8080/更改为http://mywebappname.azurewebsites.net/

Note that the web app is deployed into a different AD tenant to the one where the app registrations and [API3] are located, but I didn't think this mattered. 请注意,Web应用程序已部署到与应用程序注册和[API3]所在的AD租户不同的租户,但是我认为这并不重要。

Any thoughts out there on what I might need to change when I deploy, or how to troubleshoot this further? 是否对部署时可能需要更改的内容有任何想法,或者如何进一步解决此问题?

Update: Request works with Curl 更新:请求与Curl一起使用

Making the same request using curl is working: 使用curl发出相同的请求有效:

curl -H "Authorization: Bearer ey..." http://mywebapi.azurewebsites.net/api/resource

So the issue appears to be how I'm making the request in my C# code? 所以问题似乎是我如何在C#代码中发出请求? Comparing the headers in Fiddler, I don't see any difference. 比较Fiddler中的标题,我看不出任何区别。 This is the Fiddler trace from curl that is working: 这是来自curl的Fiddler跟踪正在起作用:

GET http://mywebapi.azurewebsites.net/api/resource HTTP/1.1
Host: mywebapi.azurewebsites.net
User-Agent: curl/7.46.0
Accept: */*
Connection: Keep-Alive
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJ<snip>

This is the Fiddler trace from my code that is not working: 这是我的代码中的Fiddler跟踪无法正常工作:

GET http://mywebapi.azurewebsites.net/api/resource HTTP/1.1
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJ<snip>
Accept: */*
User-Agent: RestSharp/100.0.0.0
Host: mywebapi.azurewebsites.net
Accept-Encoding: gzip, deflate

Here is the request from C#: 这是来自C#的请求:

var restClient = new RestClient(serviceUrl) { Timeout = timeout};
var restRequest = new RestRequest(apiEndpoint, Method.GET);
var bearerToken = $"Bearer {securityToken}";
restRequest.AddParameter("Authorization", bearerToken, ParameterType.HttpHeader);
var response = restClient.Execute(restRequest);

On the Azure web site logs, I can see that the authentication type for the successful curl request is "JWT", however for the failed requests from my code they are "anonymous". 在Azure网站日志上,我可以看到成功的curl请求的身份验证类型为“ JWT”,但是对于来自我的代码的失败请求,它们的身份验证类型为“匿名”。 Somehow the header must be being stripped despite it showing up correctly in the Fiddler trace? 尽管标头正确显示在Fiddler跟踪中,但仍必须以某种方式剥离标头? Is this possible? 这可能吗?

In an unrelated issue, I had to delete all untrusted certificates from my machine (Internet Options->Content->Certificates). 在一个不相关的问题中,我必须从我的计算机上删除所有不受信任的证书(Internet选项->内容->证书)。 And I noticed after doing this, my problem was resolved. 我注意到这样做之后,我的问题就解决了。 It was a very long list of certificates, so I don't know which one(s) were causing the problem, or why. 这是一堆非常长的证书,因此我不知道是哪个证书导致了问题,或者为什么。
Given the lack of responses, its obviously a very obscure issue, but unfortunately we didn't get to the bottom of it. 鉴于缺乏回应,这显然是一个非常晦涩的问题,但不幸的是,我们没有深入了解它。 If it occurs again, I can be a bit more methodical when I do it. 如果再次发生,执行此操作时我会变得更有条理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure中的Web API:此请求的授权被拒绝 - web API in Azure: Authorization has been denied for this request 通过 Web 请求回复 Azure 机器人 - 此请求的授权已被拒绝 - Reply to an Azure Bot via Web Request - Authorization has been denied for this request 此请求的授权已被拒绝 - 桌面到 ASP.NET Web API - Authorization has been denied for this request - Desktop to ASP.NET Web API &#39;Microsoft.Rest.Azure.CloudException&#39;,原因:&#39;Microsoft.Rest.Azure.CloudException:此请求的授权已被拒绝 - 'Microsoft.Rest.Azure.CloudException', reason: 'Microsoft.Rest.Azure.CloudException: Authorization has been denied for this request 此请求的授权被拒绝 - Azure Active Directory - 可用的日志是什么? - Authorization has been denied for this request - Azure Active Directory - What logs are available? 使用Azure Active Directory的少数用户“对此请求的授权被拒绝” - “Authorization has been denied for this request” for few Users using Azure Active Directory MSEAMS BOT - 使用 rest api 将消息发布到个人聊天 - 此请求的授权已被拒绝 - MSTEAMS BOT - post message to personalChat using rest api - Authorization has been denied for this request Azure web API 授权 - Azure web API authorization 部署在Azure Web App / Azure API上时缺少CORS标头 - CORS headers missing when deployed on Azure Web App / Azure API 通过令牌访问 Azure Web App 时拒绝访问 - Getting access denied when accessing Azure Web App via token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM