如何明确授予从用户管理身份到 AAD 应用程序注册的访问权限?

时间:2021-04-19 21:51:31

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

我们有一个用户托管标识,Service Fabric 集群使用该标识对部署到集群的服务中的各种资源进行身份验证。用例之一是使用以下代码片段向我们也拥有并通过 AAD 应用程序注册保护的 API 请求访问令牌,其中 ResourceId 参数是 AAD 应用程序注册客户端 ID:

        AppAuthenticationResult tokenResult;

        try
        {
            tokenResult = await this.azureServiceTokenProvider.GetAuthenticationResultAsync(resourceId).ConfigureAwait(false);
        }

这运行良好,但是我们最近需要通过打开关联企业应用程序上的“需要用户分配”属性来禁用对 AAD 应用程序注册的访客访问。这样做会导致上述令牌请求失败并出现异常:

Tried to get token using Managed Service Identity. Access token could not be acquired. Received a non-retryable error. MSI ResponseCode: BadRequest, Response: {"error":"invalid_grant","error_description":"AADSTS501051Application '<Managed Identity Client ID GUID>'() is not assigned to a role for the application '<AAD Application Registration Client ID GUID>' 

我们已经翻阅了文档,但似乎无法找到如何从托管身份将应用程序角色分配给 AAD 应用程序。这是如何实现的?

2 个答案:

答案 0 :(得分:1)

在您的 Web API expose App roles 中,如此处所述。

使用 this blog 中的说明将此应用角色分配给您的托管身份。

答案 1 :(得分:1)

既然您拥有 configured an application to require user assignment,您需要为您的 AAD 应用程序注册分配用户管理的身份。

This part 不适用于您的场景,因为您的不是用户,而是用户管理的身份(应用程序)。

因此,您需要在 AAD 应用程序注册中create an app role 的类型为 Application

然后使用 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"
}

我的 previous answer 中的详细步骤。