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}");
}