首页 > 解决方案 > 具有设备授权授权类型的 Azure 资源管理的范围是什么?

问题描述

我正在构建一个用于预配 Azure 资源的 CLI 应用程序。以前我使用resource设置为https://management.azure.com/. 现在,我想切换到使用 RFC 8628 设备授权授权类型(Azure 文档)。我可以使用openid profile. 但是,当我使用像https://management.azure.com我这样的范围时,会出现错误:

{
  "error": "invalid_scope",
  "error_description": "AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid. The scope openid https://management.azure.com/ is not valid. The scope format is invalid. Scope must be in a valid URI form <https://example/scope> or a valid Guid <guid/scope>.\r\n[..]",
  "error_codes": [70011],
}

我正在发送一个 POST 请求,其主体client_id=<client-id>&scope=openid+https%3A%2F%2Fgraph.microsoft.com%2F.defaulthttps://login.microsoftonline.com/<tenant>/oauth2/v2.0/devicecode. 使用这些范围,我可以很好地登录,但是对 Azure 资源管理 API(例如,对DELETE资源组)的任何后续请求都将失败,并显示401 Unauthorized.

标签: azureoauth-2.0azure-active-directory

解决方案


如果您想使用设备代码流访问 azure 资源,请按照以下步骤操作。

Azure Active Directory1.在门户中导航到您的 AD 应用程序-> API permissions-> Add a permission-> 选择Azure Service ManagementAPI -> 选择user_impersonation.

在此处输入图像描述

2.导航到门户中的订阅 -> Access control (IAM),确保您用于登录的用户帐户Contributor在订阅中具有角色。如果没有,请将用户添加为订阅中的角色,按照此文档操作。

在此处输入图像描述

3.在邮递员中,使用下面的请求。

请求网址:

POST https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/devicecode

请求正文:

client_id=<client-id>
scope=https://management.azure.com/user_impersonation

在此处输入图像描述

在浏览器中,导航到https://microsoft.com/devicelogin,输入代码并登录您的用户帐户,应用程序将让您同意权限,单击Accept

在此处输入图像描述

4.登录成功后,在邮递员中,使用下面的请求。

请求网址:

POST https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token

请求正文:

grant_type: urn:ietf:params:oauth:grant-type:device_code
client_id: <client-id>
device_code: <device_code in the screenshot of step 3>

在此处输入图像描述

5.使用access_token第4步中的调用Azure REST API,例如Resource Groups - List,它工作正常。

在此处输入图像描述

更多详情,您可以参考 - Microsoft 身份平台和 OAuth 2.0 设备授权授权流程


此外,要在步骤 3 中成功同意权限,请确保您的租户中的以下设置(Azure AD -> Enterprise applications-> User settings-> )设置为,否则,您需要让您的管理员单击步骤 1 中的按钮。Users can consent to apps accessing company data on their behalfYesGrant admin consent for xxxx

在此处输入图像描述


推荐阅读