在 Azure 文件中使用控制平面 api 创建共享和使用数据平面 api 创建文件的正确范围是什么?

问题描述 投票:0回答:1

我成功使用控制平面 API 创建了共享:

PUT /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/fileServices/default/shares/sharename?api-version=2023-01-01 HTTP/1.1
Host: management.azure.com
Authorization: Bearer token
Accept-Encoding: gzip, deflate
Connection: close
Content-Length: 17
x-ms-date: Thu, 18 Apr 2024 09:26:55 GMT
x-ms-version: 2023-01-01

,但是当我使用相同的不记名令牌在此共享中创建文件时,出现错误“服务器错误 [AuthenticationFailed]:服务器无法对请求进行身份验证。请确保授权标头的值格式正确,包括签名。” .

创建文件的 HTTP 请求:

PUT /sharename/original.dat HTTP/1.1
Host: ***.file.core.windows.net
Authorization: Bearer token
Accept-Encoding: gzip, deflate
Connection: close
Content-Length: 0
x-ms-type: file
x-ms-content-length: 26214400
x-ms-version: 2022-11-02
x-ms-file-request-intent: backup

如果我将 OAuth 范围更改为“offline_access https://{account}.file.core.windows.net/user_impersonation”并获取用于创建文件的新承载令牌,我将成功创建文件。

那么,

a) 承载令牌是否从 OAuth 范围获取:“https://management.azure.com/user_impersonation”不能用于数据平面 API?

b) 如何通过 OAuth 身份验证创建共享和创建文件?我尝试使用像这样的多个 OAuth 范围

"offline_access https://{account}.file.core.windows.net/user_impersonation https://management.azure.com/user_impersonation"
我收到错误“使用提供的授权代码请求访问令牌时,输入参数范围的提供值不能为空。请指定有效范围。”。

azure azure-files
1个回答
0
投票

承载令牌是否从 OAuth 范围获取:“https://management.azure.com/user_impersonation”不能用于数据平面 API?

不,您不能使用具有 Azure 管理 API 范围的 承载令牌 进行数据平面 API 调用,这会引发如下

Audience failed
错误:

PUT https://{account}.file.core.windows.net/sharename/testfile.dat

enter image description here

如何通过一次 OAuth 身份验证创建共享和创建文件?

不可能通过多个 OAuth 范围获取单个访问令牌。令牌生成的 scope 应与 API 调用的

host
值匹配。

当我尝试生成具有多个 OAuth 范围的令牌时,我也遇到了类似的错误,如下所示:

POST https://login.microsoftonline.com/tenantID/oauth2/v2.0/token
grant_type:authorization_code
client_id:appID
client_secret:secret
scope:offline_access https://{account}.file.core.windows.net/user_impersonation https://management.azure.com/user_impersonation
code:code
redirect_uri: https://jwt.ms

回复:

enter image description here

到目前为止,唯一的方法是单独生成不记名令牌,其中scope与API调用的

host
值相匹配,如下所示:

POST https://login.microsoftonline.com/tenantID/oauth2/v2.0/token
grant_type:authorization_code
client_id:appID
client_secret:secret
scope: https://{account}.file.core.windows.net/user_impersonation
code:code
redirect_uri: https://jwt.ms

回复:

enter image description here

您可以在 jwt.ms 网站中解码此令牌并检查

aud
声明值是否与 API 的 host 值匹配:

enter image description here

当我使用此令牌在文件共享中创建文件时,我得到了带有 201 Created 代码的

响应

PUT /testshare/testfile.dat HTTP/1.1
Host: ***.file.core.windows.net
Authorization: Bearer <token>
Accept-Encoding: gzip, deflate
Connection: close
Content-Length: 0
x-ms-type: file
x-ms-content-length: 26214400
x-ms-version: 2022-11-02
x-ms-file-request-intent: backup

回复:

enter image description here

为了确认这一点,我在 Portal 中检查了相同的内容,其中 file 已成功创建,如下所示:

enter image description here

参考: 具有多个 API 范围的访问令牌 - Stack Overflow by juunas

© www.soinside.com 2019 - 2024. All rights reserved.