如何使用OpenID和Microsoft Graph枚举Azure Active Directory组?

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

我有以下价值观:

如何使用这些值来创建Microsoft Graph服务客户端?

var graphClient = new GraphServiceClient(
    // What goes here?
);

我需要客户端枚举AAD组。

c# azure-active-directory microsoft-graph
1个回答
1
投票

根据您的描述,我假设您使用的是AAD v1.0,要使用Microsoft Graph客户端SDK,您需要为Microsoft Graph API添加必需的权限,其中包含Azure Portal上AAD应用程序的应用程序权限或委派权限。应用程序权限和委派权限之间的差异,您可以遵循here

对于Web应用程序并使用基于用户的身份验证流程,您可以按照以下示例进行操作:

Calling the Azure AD Graph API in a web application

Microsoft Graph Snippets Sample for ASP.NET 4.6

注意:对于您的方案,您需要组合上述两个示例中的代码。或者您可以创建AAD v2.0应用程序,然后使用第二个示例。

对于服务器到服务器方案,您可以使用ADAL检索访问令牌以初始化您的GraphServiceClient

    private static async Task<string> GetAccessTokenAsync()
    {
        string tenantId = "<tenantId>";
        string clientId = "<clientId>";
        string clientSecrets = "<clientSecrets>";
        Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult result = null;

        var context = new AuthenticationContext(String.Format("https://login.windows.net/{0}", tenantId)); 

        var authParam = new PlatformParameters(PromptBehavior.Never, null);
        var result = await context.AcquireTokenAsync(
                "https://graph.microsoft.com"
                , new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientId, clientSecrets)
                );
        return result.AccessToken;
    }

//initialize the GraphServiceClient instance
var graphClient = new GraphServiceClient(
            "https://graph.microsoft.com/v1.0",
            new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    var token = await GetAccessTokenAsync();
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                }));
© www.soinside.com 2019 - 2024. All rights reserved.