Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

59 lines (50 sloc) 2.7 KB
using System;
using System.Collections.Generic;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using Microsoft.Identity.Client;
namespace BTJ.CSAdvent.AZFunc
{
public static class GetUserMicrosoftGraph
{
private static GraphServiceClient _graphServiceClient;
[FunctionName("GetUserMicrosoftGraph")]
public static void Run([TimerTrigger("%timerSchedule%")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
//Query using Graph SDK (preferred when possible)
GraphServiceClient graphClient = GetAuthenticatedGraphClient();
List<QueryOption> options = new List<QueryOption>
{
new QueryOption("$top", "1")
};
var graphResult = graphClient.Users.Request(options).GetAsync().Result;
log.LogInformation("Graph SDK Result");
log.LogInformation(graphResult[0].DisplayName);
}
private static GraphServiceClient GetAuthenticatedGraphClient()
{
var authenticationProvider = CreateAuthorizationProvider();
_graphServiceClient = new GraphServiceClient(authenticationProvider);
return _graphServiceClient;
}
private static IAuthenticationProvider CreateAuthorizationProvider()
{
var clientId = System.Environment.GetEnvironmentVariable("AzureADAppClientId", EnvironmentVariableTarget.Process);
var clientSecret = System.Environment.GetEnvironmentVariable("AzureADAppClientSecret", EnvironmentVariableTarget.Process);
var redirectUri = System.Environment.GetEnvironmentVariable("AzureADAppRedirectUri", EnvironmentVariableTarget.Process);
var tenantId = System.Environment.GetEnvironmentVariable("AzureADAppTenantId", EnvironmentVariableTarget.Process);
var authority = $"https://login.microsoftonline.com/{tenantId}/v2.0";
//this specific scope means that application will default to what is defined in the application registration rather than using dynamic scopes
List<string> scopes = new List<string>();
scopes.Add("https://graph.microsoft.com/.default");
var cca = ConfidentialClientApplicationBuilder.Create(clientId)
.WithAuthority(authority)
.WithRedirectUri(redirectUri)
.WithClientSecret(clientSecret)
.Build();
return new MsalAuthenticationProvider(cca, scopes.ToArray());
}
}
}
You can’t perform that action at this time.