/
GraphServiceHelper.cs
40 lines (35 loc) · 1.29 KB
/
GraphServiceHelper.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
using Microsoft.Graph;
using System.Net.Http.Headers;
using ToDoGraphDemo.Auth;
namespace ToDoGraphDemo
{
/// <summary>
/// A helper class for acquiring an authenticated GraphServiceClient for interacting with Graph API.
/// </summary>
public class GraphServiceHelper
{
// GraphServiceClient singleton.
private static GraphServiceClient client = null;
// Returns a GraphServiceClient with the user's access token set to the HTTP auth header.
public static GraphServiceClient GetAuthenticatedClient()
{
if (client == null)
{
client = new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
// Get access token.
string accessToken = await GraphAuthProvider.Instance.GetUserAccessTokenAsync();
// Set access token to HTTP auth header.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}));
}
return client;
}
public static void SignOut()
{
client = null;
}
}
}