Access to the GraphAPI requires an auth token best collected via ADAL.NET MSAL.NET is an alternative but it does not handle caching elegantly
- Login to portal.azure.com
- Azure AD
- Visit the app registration
- Settings => Required Permissions
- Add Graph API if it does not already appear in the list
- Add the appropriate permissions (as an app typically)
- Grant the permissions
private async Task<GraphServiceClient> GetGraphServiceClient()
{
var ctx = new AuthenticationContext(($"https://login.microsoftonline.com/{_domain}/"));
var clientCredential = new ClientCredential(_clientId, _clientSecret);
var token = await ctx.AcquireTokenAsync("https://graph.microsoft.com/", clientCredential);
var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
return Task.CompletedTask;
}));
return graphServiceClient;
}
Get-AzureADUser -ObjectId $UserId | Select -ExpandProperty ExtensionProperty