因此,我的要求是让在 Azure 计算机上运行的应用程序检查登录的特定 Azure 用户是否属于指定的 Azure 组。
我已为上述虚拟机启用托管服务身份。 MSI
我通过对 VM 上的图形资源使用以下命令调用本地运行的元数据服务来获取 VM 的 token :
curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fgraph.windows.net' -H Metadata:true
一旦获得 token ,我就会向 isMemberOf 的图形 API 发出 POST 请求。使用新获取的 token :
curl -i -H "Authorization: Bearer <token-value>" -H "Content-Type: application/json" --data '{"groupId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","memberId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}' https://graph.windows.net/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/isMemberOf?api-version=1.6
我收到以下回复
{"odata.error":{"code":"Authorization_RequestDenied","message":{"lang":"en","value":"Insufficient privileges to complete the operation."}}}
我已分配虚拟机所属的资源组,并将 IAM 作为资源的所有者
我是否遗漏了一些我应该配置的东西,或者我在这里做错了什么?
最佳答案
所以我仔细研究了一下,发现微软更喜欢我们开发人员使用 Microsoft Graph 而不是 Azure AD Graph Microsoft Graph vs Azure AD Graph
我在 Microsoft Graph API 中发现了类似的调用来获取成员信息 User list member of call 此调用需要以下 scope :
Directory.Read.All
Directory.ReadWrite.All
这些是我遵循的步骤:
- 应用程序需要获取 token 才能进行 Microsoft 图形 API 调用。
发布https://login.microsoftonline.com/ {租户-id}/oauth2/v2.0/token
标题 内容类型:application/x-www-form-urlencoded
正文: client_id=&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=&grant_type=client_credentials
回应:
{
"token_type": "Bearer",
"expires_in": 3600,
"ext_expires_in": 0,
"access_token": "<token-value>"
}
客户端 key 是在应用程序注册时生成的。
- 应用程序将调用 Microsoft Graph API 来获取
发布https://graph.microsoft.com/v1.0/users/ {user-object-id}/checkMemberGroups
header :授权:持有者{ token 值} 内容类型:application/json
正文:
{
"groupIds":["xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"]
}
回应:
200 OK - 如果用户属于该组
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(Edm.String)",
"value": [
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
]
}
404 未找到 - 如果用户不存在
{
"error": {
"code": "Request_ResourceNotFound",
"message": "Resource 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' does not exist or one of its queried reference-property objects are not present.",
"innerError": {
"request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"date": "2018-05-23T02:54:47"
}
}
}
这将帮助应用程序识别 Azure 用户是否属于指定的组。
关于rest - 尝试从 Azure 虚拟机访问时,Azure isMemberOf api 出现 "Insufficient privileges to complete the operation."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50393742/