为Azure Active Directory系统托管身份分配自定义角色

时间:2020-07-28 17:19:03

标签: azure-active-directory azure-managed-identity

为了在APIS之间建立Azure AD系统托管身份,我在应用程序清单中为目标API定义了自定义角色。

"appRoles": [
    {
        "allowedMemberTypes": [
            "Application"
        ],
        "description": "Allow the application to read all things as itself.",
        "displayName": "Read all things",
        "id": "86a914fa-a862-4962-9975-be5c9a05dca3",
        "isEnabled": true,
        "lang": null,
        "origin": "Application",
        "value": "Things.Read.All"
    }

现在,我想将此角色分配给要调用它的api,以便可以在从AzureServiceTokenProvider接收的访问令牌中对其进行验证。 问题是我在应用程序注册中看不到系统分配的身份。

“身份”(声明系统已分配身份的位置)下方有一个“ Azure角色分配”按钮,可用于添加角色分配。这里有可用的角色列表。我正在寻找自己定义的自定义角色,它不在下拉菜单中。

如何将已定义的角色分配给系统身份,以便它可以访问允许的api或不再可用的api?我希望在访问令牌中获得此角色。这是正确的期望吗?

1 个答案:

答案 0 :(得分:2)

您定义的是app role。但是“ Azure角色分配”用于分配要订阅的角色。它们完全是两件事。

您可以使用Microsoft Graph API来Grant an appRoleAssignment to a service principal

POST https://graph.microsoft.com/v1.0/servicePrincipals/{id}/appRoleAssignedTo
Content-Type: application/json
Content-Length: 110

{
  "principalId": "principalId-value",
  "resourceId": "resourceId-value",
  "appRoleId": "appRoleId-value"
}

在此示例中,{id}{resourceId-value}都将是资源服务主体的对象ID,该主体是与您在其中创建appRoles的Azure AD应用程序关联的企业应用程序。像这样:

enter image description here

enter image description here

{principalId-value}将是Azure资源托管身份的ID。在这里找到它:

enter image description here

{appRoleId-value}是您在清单中创建的应用角色的ID。

enter image description here

您可以使用管理员帐户登录Microsoft Graph Explorer来调用Microsoft Graph API。

如果要验证结果是否成功,请导航至 Azure门户-> Azure Active Directory -> 企业应用程序- > 所有应用程序。输入Azure资源的名称。

enter image description here

然后您将发现已授予应用程序角色(应用程序许可权)。

enter image description here