简体   繁体   中英

Query Azure AD with GraphServiceClient

I want to query Azure Active Directory For Users with GraphServiceClient.

I registered an application on the Azure portal and get back a token. Now I want to GetUsers of my ActiveDirectory but dont understand how to query the Azure AD

  static async void Works() //gets a token 
        {
            try
            {
                var tenantId = "myTenant";
                var clientId = "myId";
                var clientSecret = @"MySecret";

                // Configure app builder
                var authority = $"https://login.microsoftonline.com/{tenantId}";
                var app = ConfidentialClientApplicationBuilder
                    .Create(clientId)
                    .WithClientSecret(clientSecret)
                    .WithAuthority(new Uri(authority))
                    .Build();

                // Acquire tokens for Graph API
                var scopes = new[] { "https://graph.microsoft.com/.default" };
                var authenticationResult = await app.AcquireTokenForClient(scopes).ExecuteAsync();

                // Create GraphClient and attach auth header to all request (acquired on previous step)
                var graphClient = new GraphServiceClient(
                    new DelegateAuthenticationProvider(requestMessage =>
                    {
                        requestMessage.Headers.Authorization =
                            new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", authenticationResult.AccessToken);

                        return Task.FromResult(0);
                    }));

                // Call Graph API
                //HOW DO I QUERY AZURE AD????
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }

If you want to list the users, you could use the code as below.

 var users = await graphClient.Users
                .Request()
                .GetAsync();

在此处输入图片说明

For more details, refer to this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM