我们正在测试通过 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。
编辑:
正如 Wayne 所说,您还可以将 scope
设置为 {your_client_id}/User.Read
来获取访问 token 。
关于azure - 通过 token 访问 Azure Web App 时访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57298188/