使用AAD身份验证的Azure存储列表Blob-受众群体验证失败

时间:2020-04-23 13:52:42

标签: azure rest authentication azure-storage-blobs

使用邮递员,获得一个Bearer令牌,该令牌可以成功列出存储帐户和资源组。在同一集合中,尝试列出容器中的Blob并获得“受众验证失败。受众不匹配”的信息。诊断此错误的任何帮助将不胜感激。 enter image description here

1 个答案:

答案 0 :(得分:0)

如果要使用Azure AD访问令牌来访问Azure Blob Rest API,我们需要分配Azure RABC角色(存储Blob数据所有者存储Blob数据贡献者 Storage Blob数据读取器)来服务主体或AD用户。有关更多详细信息,请参阅document

例如(我使用服务主体) 1.创建服务主体并为sp分配Azure RABC角色。

az login
az account set --subscription "<your subscription id>"
# it will assign Storage Blob Data Reader to the sp at storage accountlevel
az ad sp create-for-rbac -n "mysample" --role Storage Blob Data Reader --scopes <the resource id of storage account>

enter image description here

  1. 获取广告访问令牌
POST /<your sp tenant id>/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded

grant_type =client_credentials
&client_id=<your sp appId>
&client_secret=<your sp password>k
&resource=https://storage.azure.com/
  1. 列出斑点
GET https://myaccount.blob.core.windows.net/mycontainer?restype=container&comp=list

x-ms-version: 2017-11-09
Authorization: Bearer <access token>

enter image description here