-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphServiceClientProvider.cs
66 lines (60 loc) · 3.54 KB
/
GraphServiceClientProvider.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Configuration;
namespace Helpers
{
class GraphServiceClientProvider
{
// The client ID is used by the application to uniquely identify itself to the authentication endpoint.
private static string clientIdAppSet = WebConfigurationManager.AppSettings["clientId"].ToString();
private static string tenantIdAppSet = WebConfigurationManager.AppSettings["tenantId"].ToString();
private static string clientSecretAppSet = WebConfigurationManager.AppSettings["clientSecret"].ToString();
private static string authorityFormatAppSet = WebConfigurationManager.AppSettings["authorityFormat"].ToString();
private static string msGraphScopeAppSet = WebConfigurationManager.AppSettings["msGraphScope"].ToString();
private static string redirectUriAppSet = WebConfigurationManager.AppSettings["redirectUri"].ToString();
private static PublicClientApplication identityClientApp = new PublicClientApplication(clientIdAppSet);
private static GraphServiceClient graphClient = null;
private static AuthenticationResult authResult = null;
// Get an access token for the given context and resourceId. An attempt is first made to acquire the token silently.
// If that fails, then we try to acquire the token by prompting the user.
public static GraphServiceClient GetAuthenticatedClient()
{
if (graphClient == null)
{
try
{
graphClient = new GraphServiceClient(
"https://graph.microsoft.com/v1.0",
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
//var token = authResult!= null ? authResult.AccessToken : await getTokenForUserAsync();
string clientId = clientIdAppSet;
string authorityFormat = authorityFormatAppSet;
string tenantId = tenantIdAppSet;
string msGraphScope = msGraphScopeAppSet;
string redirectUri = redirectUriAppSet; // Custom Redirect URI asigned in the Application Registration Portal in the native Application Platform
string clientSecret = clientSecretAppSet;
ConfidentialClientApplication daemonClient = new ConfidentialClientApplication(clientId, String.Format(authorityFormat, tenantId), redirectUri, new ClientCredential(clientSecret), null, new TokenCache());
AuthenticationResult authResult = await daemonClient.AcquireTokenForClientAsync(new string[] { msGraphScope });
string token = authResult.AccessToken;
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
}
));
return graphClient;
}
catch (Exception error)
{
Debug.WriteLine($"Could not create a graph client {error.Message}");
}
}
return graphClient;
}
}
}