I'm developping a tool (Windows Forms) to automate certain processes. I need to read the members of specific AD group and create an event in each members calendar. For this I use the GraphServiceClient in a C# application.
- I've added an "App registration" in the Azure Portal: Jansen Automation
- I've added all permissions needed AND granted admin consent
- I've created a ClientSecret a copied the value of it to my code
Next I want to read all groups from AD, using the example code from https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#tabpanel_2_CS:
private GraphServiceClient GetClient()
{
try
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = _options.TenantId;
// Values from app registration
var clientId = _options.ClientId;
var clientSecret = _options.ClientSecret;
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
return graphClient;
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
throw;
}
}
public async Task<List<string>> GetAllGroups()
{
try
{
var client = GetClient();
var groups = await client.Groups
.Request()
.Header("ConsistencyLevel", "eventual")
.GetAsync();
return groups?.Select(g => g.DisplayName).ToList();
}
catch (Exception ex)
{
throw;
}
}
The result is an exception:
What am I missing here?