Retrieving group members from Azure Active Directory

Pako Porras 81 Reputation points
2020-07-30T12:48:46.953+00:00

Hello:

I have a NET Core 3.0 application that log users against Azure AD. I want to know how I can retrieve all members from a group, because I need to send an email to all members of that group. My understanding is that this can be done using Microsoft Graph, but I don't know how to do it and if I need to configurate permissions on Azure inside my enterprise applications.

Could you provide me with help about this? Any code example?

Thanks

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,460 questions
0 comments No comments
{count} votes

Accepted answer
  1. 2020-07-30T13:55:51.887+00:00

    Using Azure.Identity and Microsoft.Graph nuget packages. Ensure required permissions are set and requested to the application.

       var tokenProvider = new DefaultAzureCredential(true);  
         
       var client = new GraphServiceClient(  
       new DelegateAuthenticationProvider(async context = >{  
       var token = (await tokenProvider.GetTokenAsync(new TokenRequestContext(new[] {  
       "https://graph.microsoft.com/.default"  
       }))).Token;  
       context.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);  
       }));  
         
       var groups = await client.Groups.Request().Top(5).Expand("members").GetAsync();  
         
       foreach(var item in groups) {  
       Console.WriteLine($ "Group {item.Id} member count: {item.Members.Count}");  
       }  
    

0 additional answers

Sort by: Most helpful