-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAccessGraphWithMsal.cs
47 lines (42 loc) · 1.46 KB
/
AccessGraphWithMsal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
namespace FunctionApp.Functions
{
/// <summary>
/// This variant uses an MSAL based authentication with supports caching of token (recommended solution)
/// Adapted from <see href="https://docs.microsoft.com/en-us/graph/tutorials/azure-functions"/>
/// </summary>
public static class AccessGraphWithMsal
{
private static GraphServiceClient _graphClient;
[SuppressMessage("Microsoft.Performance", "IDE0060:ReviewUnusedParameters")]
[FunctionName(nameof(AccessGraphWithMsal))]
public static async Task RunAsync(
[TimerTrigger("%" + Constants.Configurations.LoadFuncSchedule + "%")] TimerInfo timer,
ILogger log,
CancellationToken cancellationToken)
{
if (!Configurations.BuiltInAuth.IsEnabled)
{
log.LogWarning("Function is not linked with an app registration");
return;
}
if (_graphClient == null)
{
// Create a client credentials auth provider
var authProvider = new BuiltInAuthProvider(
new[] { Constants.AuthScopes.Graph },
log);
_graphClient = new GraphServiceClient(authProvider);
}
// Assign application permission 'Organisation.Read.All' for testing
var organisation = await _graphClient.Organization.Request().GetAsync(cancellationToken);
Console.WriteLine(organisation);
}
}
}