【问题标题】:Azure AD Access token of Client Application does not contain Roles Claims客户端应用程序的 Azure AD 访问令牌不包含角色声明
【发布时间】:2023-01-11 15:17:23
【问题描述】:

我有 2 个 Azure AD 应用程序在 Azure AD 应用程序注册中说 Client-AppServer-App

服务器广告应用程序:

  • 在 Azure AD 中注册了一个新的应用程序。
  • 设置名为“Search.Address”的应用程序角色,这是自定义角色。

客户广告申请:

  • 在 Azure AD 中注册了一个新的应用程序。
  • API 权限:添加了在server-app 注册中创建的角色“Search.Address”在client app 中作为应用程序权限公开。
  • 已成功授予管理员访问权限。

我有使用 .NET 堆栈创建的客户端 Function App,并启用了与 Client-App 关联的系统管理身份。客户端功能应用程序运行代码以使用 ManagedIdentityCredential 获取访问令牌。

令牌创建成功但缺少角色“Search.Address”。

我尝试将 Client-App 作为 API 公开。但没有白费。

托管身份是否有权与服务器对话?我如何使用 approleassignment 分配它?

 public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;
            string responseMessage = string.Empty;
            try
            {

               var credential = new ManagedIdentityCredential();
               var accessToken = await credential.GetTokenAsync(new TokenRequestContext(scopes: new string[] { "SERVERAPP_ClientID/.default" }) { });
               
                responseMessage = string.IsNullOrEmpty(name)
                    ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                    : $"Hello, {name}. Your Token: {accessToken.Token}";
            }
            catch (Exception ex)
            {

                log.LogError(ex.Message+ex.InnerException.Message);
            }           

            return new OkObjectResult(responseMessage);
        }
    }

参考:

https://anoopt.medium.com/few-ways-of-obtaining-access-token-in-azure-application-to-application-authentication-40a9473a2dde

【问题讨论】:

    标签: oauth-2.0 azure-active-directory azure-functions access-token azure-managed-identity


    【解决方案1】:

    您需要将应用程序权限/应用程序角色分配给托管身份服务主体。 目前您无法通过门户网站执行此操作,您需要 PowerShell 来执行此操作:

    Connect-AzureAD
    New-AzureADServiceAppRoleAssignment -ObjectId 1606ffaf-7293-4c5b-b971-41ae9122bcfb -Id 32028ccd-3212-4f39-3212-beabd6787d81 -PrincipalId 1606ffaf-7293-4c5b-b971-41ae9122bcfb -ResourceId c3ccaf5a-47d6-4f11-9925-45ec0d833dec
    

    请注意,这是针对较旧的 AzureAD 模块。较新的 Microsoft.Graph 模块也有类似的 cmdlet。

    对于 AzureAD cmdlet,您需要的 ID 是:

    • ObjectId 和 PrincipalId:托管身份服务主体对象 ID
    • id:应用角色/应用权限的id
    • ResourceId:API 服务主体的对象 ID

    运行此命令与管理员同意应用程序权限是一回事。

    我写的文章:https://joonasw.net/view/calling-your-apis-with-aad-msi-using-app-permissions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 2018-02-07
      • 2020-02-13
      • 2022-12-01
      • 2019-09-13
      • 2020-01-16
      • 2018-12-31
      相关资源
      最近更新 更多