azure - 通过 token 访问 Azure Web App 时访问被拒绝

标签 azure oauth-2.0 azure-active-directory postman azure-web-app-service

我们正在测试通过 Azure Active Directory 使用内置的 Azure Web 应用身份验证/授权来锁定 Azure Web 应用上托管的 API。

我们在 Postman 中使用以下参数调用 POST https://login.microsoftonline.com/{tenantIDhere}/oauth2/v2.0/token:

grant_type: password
client_id: {ID}
username: {username}@{tenenat}.onmicrosoft.com
password: {password}
scope: https://{webappname}.azurewebsites.net/.default
client_secret: {secret}

以下是我得到的回复:

"token_type":"Bearer","scope":"https://{webappname}.azurewebsites.net/user_impersonation https://{webappname}.azurewebsites.net/.default","expires_in":3599,"ext_expires_in":3599,"access_token":"{TOKEN HERE}"}

因此,我获取该 token 并尝试通过在 header 中传递 token 来访问网页,然后使用 POST 收到以下错误:

https://{webappname}.azurewebsites.net/api/url

Authorization: Bearer {TOKEN HERE}

响应(401未经授权)

You do not have permission to view this directory or page.

我花了几天时间尝试我能找到的一切。我缺少什么?一旦我关闭网络应用程序所需的授权,我就会得到预期的结果。但是使用 grant_type: 密码,我无法进入!我有一种感觉,它与范围有关,但我找不到我需要的文档。有什么想法吗?

用户可以手动登录网页并拉取数据,因此不是权限问题。我认为这与代币有关。

最佳答案

如果您使用 v2.0 端点,范围应为 {your_client_id}/.default。将 https://{webappname}.azurewebsites.net 替换为 Azure AD 中 API 应用的客户端 ID/应用程序 ID。

enter image description here

编辑:

正如 Wayne 所说,您还可以将 scope 设置为 {your_client_id}/User.Read 来获取访问 token 。

关于azure - 通过 token 访问 Azure Web App 时访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57298188/

相关文章:

azure - 我们可以将加密虚拟机从一个订阅转移到另一个订阅吗

api - 具有单个服务器的 OAuth 2.0

c# - ASP.NET MVC Identity - 一起使用内部用户和 Azure AD 身份验证

java - 如果刷新 token 被撤销,则从应用程序中注销用户

web-services - OAuth 2.0 多范围(客户端凭据案例)

azure - 可以和不能使用 B2C 进行身份验证的注册应用程序之间有什么区别?

ASP.NET Core 2.1 Preview 2 应用程序无法在 Azure 应用服务上运行

azure - 如何通过 VSTS 将 Azure WebJob 和应用程序部署到同一应用程序服务?

azure - 如何跨 azure 存储表中的多个分区键进行查询?

security - 使用Spring Security Rest插件保护Grails Rest Api