尝试使用 AD 令牌/承载令牌 [Azure-Blob][承载令牌] 将文件放入 Azure Blob 中时授权权限不匹配

2024-02-28

我能够创建容器、列表容器、列表Blob但当我试图做一个PUT/DELETE请求upload or deleteAzure 存储 blob 中的文件,但发出请求后显示以下错误:

403
This request is not authorized to perform this operation using this permission.
{
  'content-length': '279',
  'content-type': 'application/xml',
  server: 'Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0',
  'x-ms-request-id': '4de6c154-f01e-0051-7ce4-1314ef000000',
  'x-ms-version': '2018-03-28',
  'x-ms-error-code': 'AuthorizationPermissionMismatch',
  date: 'Mon, 08 Mar 2021 06:32:44 GMT',
  connection: 'close'
}

代码为upload/PUT文件是:

const request = require("request");
require("dotenv").config();

const account = process.env.ACCOUNT_NAME || "";
const containerName = "demo";
const blobName = "dummyfile1.txt";
const blobContent = "Hello, This will be written in file";
const contentLength = new TextEncoder().encode(blobContent).length;

var strTime = new Date().toUTCString();

const options = {
  url: `https://${account}.blob.core.windows.net/${containerName}/${blobName}`,
  headers: {
    Authorization: "Bearer <BearerToken>",
    "x-ms-date": strTime,
    "x-ms-version": "2018-03-28",
    "x-ms-blob-type": "BlockBlob",
    "Content-Length": contentLength,
    "Content-Type": 'application/text-plain',
  },
  body: blobContent,
};

function callback(error, response, body) {
  console.log(response.statusCode);
  console.log(response.statusMessage);
  console.log(response.headers);
}

request.put(options, callback);

在这里,我手动替换我通过 POSTMAN 获取的内容:

另外,我还给App添加了Storage Data Contributor的权限:

I have delegated Azure Storage, user_impersonation permission also to the Application. Azure Storage user_impersonation

但同样的错误仍然存​​在。


使用时授权码流程 https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow,您的登录用户需要 Azure 存储的权限。使用时Storage Blob Data Contributor角色,您需要添加角色分配你的帐户但不是应用程序(只有客户端凭据流需要应用程序的角色)。

然后将Azure存储权限添加到API权限中。

此外,两者https://<account-name>.blob.core.windows.net/user_impersonation and https://storage.azure.com/user_impersonation可用于范围。有关 Azure 存储资源 ID(范围)的更多详细信息,请参阅here https://learn.microsoft.com/en-us/azure/storage/common/storage-auth-aad-app?tabs=dotnet#azure-storage-resource-id.

The https://${account}.blob.core.windows.net/.default or https://storage.azure.com/.default适用于客户端凭证流。


Steps:

  1. 在浏览器中获取授权码

注意:登录 Azure 帐户后,您应该接受请求的权限。

https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/authorize?
client_id={client-id}
&response_type=code
&redirect_uri=https://localhost:44300/
&response_mode=query
&scope=https://{account}.blob.core.windows.net/user_impersonation
&state=12345
&prompt=consent
  1. 获取访问令牌和刷新令牌。尝试解码访问令牌https://jwt.io/ https://jwt.io/, 检查aud,看起来像https://xxxx.blob.core.windows.net.
  1. 最后,在代码中测试访问令牌。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试使用 AD 令牌/承载令牌 [Azure-Blob][承载令牌] 将文件放入 Azure Blob 中时授权权限不匹配 的相关文章

随机推荐