Graph API 根据 appid 列表检索多个应用程序

问题描述 投票:0回答:1

我目前正在尝试根据应用程序 ID 列表过滤 Azure 应用程序。我已经进行了必要的设置,图形服务客户端具有“Application.ReadWrite.OwnedBy”权限,并且相同的客户端应用程序作为所有者添加到我想要检索的应用程序中。我有以下基于单个 appId 进行过滤的代码片段,但我想将其修改为基于整个 appId 列表进行过滤。

我当前用于过滤单个 appId 详细信息的代码片段

var appIds = ["id1","id2","id3"]
var applications = await this.graphServiceClient.ApplicationsWithAppId(appIds[0]).GetAsync((requestConfiguration) => requestConfiguration.QueryParameters.Select = ["id", "appId", "displayName", "requiredResourceAccess"]);

我想根据整个

appIds
数组进行过滤

c# azure azure-ad-graph-api azure-service-principal
1个回答
0
投票

我注册了一个应用程序,并向其添加了 Application 类型的 “Application.ReadWrite.OwnedBy” 权限,如下所示:

enter image description here

现在,我将其作为 Owner 添加到我想要检索的应用程序中:

enter image description here

要基于整个appIds数组来

过滤
应用程序,您可以使用以下示例代码:

using Azure.Identity;
using Microsoft.Graph;

class Program
{
    static async Task Main(string[] args)
    {
        var scopes = new[] { "https://graph.microsoft.com/.default" };
        var tenantId = "tenantId";
        var clientId = "appId";
        var clientSecret = "secret";
        var appIds = new[] { "appId1", "appId2", "appId3" };

        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
        };

        var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);

        var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

        try
        {
            var filterExpression = string.Join(" or ", appIds.Select(id => $"appId eq '{id}'"));

            var applications = await graphClient.Applications
                .GetAsync(requestConfiguration =>
                {
                    requestConfiguration.QueryParameters.Filter = filterExpression;
                    requestConfiguration.QueryParameters.Select = new string[] { "appId", "displayName" };
                });

            foreach (var application in applications.Value)
            {
                Console.WriteLine($"App Name: {application.DisplayName}");
                Console.WriteLine($"App ID: {application.AppId}");
                Console.WriteLine();
            }
        }
        catch (ServiceException serviceException)
        {
            Console.WriteLine(serviceException.Message);
        }
    }
}

回复:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.