将应用程序角色分配给托管身份服务主体

Assign an app-role to a managed-identity service principal

喜欢 here, I want to secure an Azure Function app with AzureAD and only be able to call/invoke it from an Azure API Management instance (with system-assigned managed identity enabled). This works fine. However I really want to isolate access to the API (Functions app). As it stands now, ANY app in my tenant would be able to successfully authenticate and invoke the API. Therefore, I chose to enable user-assignment 该应用程序注册。

所以基本上,有 1 个 API (Azure Functions) 受 AzureAD 保护。然后有 1 个服务调用 API(Azure API 管理)。没有角色分配,我可以理解以下错误消息。

AADSTS501051: Application '<appId>'(<name of APIM managed-identity>) is not assigned to a role for the application '<appId2>'(<name of app registration for the Azure Functions app's EasyAuth feature>).

所以我在 API 的应用程序注册中创建了一个应用程序角色。有关如何将此类角色分配给应用程序的正常指南,请参见此处 (编辑:错误 LINK 请参见答案)。然而,这仅适用于手动创建的服务主体(阅读:不是 MSI),因为这需要服务主体可以在门户 UI 中找到。与管理身份相关的任何事情都不是这种情况。

由于上面的 link 还指出也可以使用 Graph API 来完成赋值,我尝试了这个作为解决方法。

POST https://graph.microsoft.com/v1.0/users/{id}/appRoleAssignments
Content-Type: application/json

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

这不会产生以下错误响应(我已尝试 id/principalId/resourceId 的 appId 和 objectId):

"Resource '<guid of principalId>' does not exist or one of its queried reference-property objects are not present."

您对可以做什么有什么建议吗?非常感谢任何帮助。

文档中的 MS-Graph link 是错误的。它指的是将角色分配给用户,尽管在 linking 文本正文中暗示了应用程序。 This 是正确的引用。

POST /servicePrincipals/{objectId}/appRoleAssignments

另外请确保,如果您使用个人帐户(例如 outlook.com、hotmail.com)创建了 Azure 订阅,则不要在图形调用中使用该用户的不记名令牌.而是在 AzureAD 中创建一个用户,分配适当的权限,并使用该帐户的令牌。