azure-storage - 在本机应用程序中使用 ClientSecretCredential 对 BlobServiceClient 进行身份验证
问题描述
我正在尝试对原生 .net 应用程序中的 azure blob 存储进行身份验证。以下代码产生 403。我没有看到任何身份验证流程被触发(例如,没有同意或 TFA 提示),但可能不应该出现这种情况。客户端注册配置为具有 user_impersonation 范围的本机应用程序。我想知道我应该采取哪些步骤来解决问题。
var credential = new ClientSecretCredential(tenantid, appid, clientSecret);
client = new BlobServiceClient(accountUri, credential);
// Make a service request to verify we've successfully authenticated
var foo= await client.GetPropertiesAsync();
回复:
Azure.RequestFailedException: This request is not authorized to perform this operation using this permission.
RequestId:73e54cff-401e-004d-7211-685a00000000
Time:2020-08-01T14:37:01.2280787Z
Status: 403 (This request is not authorized to perform this operation using this permission.)
ErrorCode: AuthorizationPermissionMismatch
Headers:
x-ms-request-id: 73e54cff-401e-004d-7211-685a00000000
x-ms-client-request-id: a9a34270-db76-424b-ac33-750b2cdb2ffb
x-ms-version: 2019-12-12
x-ms-error-code: AuthorizationPermissionMismatch
Date: Sat, 01 Aug 2020 14:37:00 GMT
Server: Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0
Content-Length: 279
Content-Type: application/xml
解决方案
如果要通过客户端凭据流访问 Azure 存储,我们需要将 Azure RABC 角色(存储 Blob 数据参与者)分配给 Azure AD 应用程序。更多详细信息,请参阅文档
例如
代码
var clientId = "42e0d***2d988c4";
var secret = "Gbx2***fQpIjoae:";
var tenant = "e4c9ab4***2a757fb";
ClientSecretCredential credential = new ClientSecretCredential(tenant, clientId, secret);
string accountName = "jimtestdiag924";
string url = string.Format("https://{0}.blob.core.windows.net/", accountName);
var client = new BlobServiceClient(new Uri(url), credential);
var foo = await client.GetPropertiesAsync();
推荐阅读
- python - 如何在数组中循环url并在驱动程序中打印css选择器元素,Python Selenium
- java - 防止颤振插件截图
- javascript - 移动导航栏打开时隐藏元素并禁用滚动
- swift - 在 Swift 中获取 ioctl 编号
- python - 缩短字典中键的长度
- python - Cloropeth folium 地图中的工具提示。并添加垂直图例
- azure-devops - 如何保留代理的临时文件夹以便手动从 Azure DevOps 发布管道调用命令
- vue.js - 使用 Vue.js 组合 API 的全局反应状态
- javascript - 无法添加新会员号
- javascript - React - 何时使用 useImperativeHandle?