尝试使用AD令牌/承载令牌将文件放入Azure Blob时授权权限不匹配[Azure-Blob][承载令牌] [英] Authorization Permission Mismatch when trying to PUT a file in Azure Blob with AD token/ Bearer Token[Azure-Blob][Bearer-Token]

查看:79
本文介绍了尝试使用AD令牌/承载令牌将文件放入Azure Blob时授权权限不匹配[Azure-Blob][承载令牌]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以CreateContainers,ListContainers,ListBlobs,但是当我尝试PUT/DELETE请求上载Azure Storage 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);

这里我通过以下方式手动更换邮递员:

另外,我还向App添加了存储数据贡献者的权限:

我还向应用程序委派了Azure Storage、USER_IMPERATION权限。

但同样的错误仍然存在。

推荐答案

使用auth code flow时,您的登录用户需要拥有Azure Storage权限。使用Storage Blob Data Contributor角色时,您需要将角色分配添加到您的帐户,而不是应用程序(只有客户端凭据流需要应用程序的角色)。

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

此外,https://<account-name>.blob.core.windows.net/user_impersonationhttps://storage.azure.com/user_impersonation都可以用于作用域。有关Azure存储资源ID(作用域)的更多详细信息,请参阅here

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


步骤:

  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/中的访问令牌,检查aud,它看起来像https://xxxx.blob.core.windows.net

  1. 最后,测试代码中的访问令牌。

这篇关于尝试使用AD令牌/承载令牌将文件放入Azure Blob时授权权限不匹配[Azure-Blob][承载令牌]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆