-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphClientFactory.cs
More file actions
83 lines (68 loc) · 2.9 KB
/
Copy pathGraphClientFactory.cs
File metadata and controls
83 lines (68 loc) · 2.9 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Diagnostics;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Common.Utils
{
public static class GraphClientFactory
{
public static GraphServiceClient GetTeamsCallLoggerGraphClient()
{
return Utils.GraphClientFactory.GetGraphClient(
Environment.GetEnvironmentVariable("client"),
Environment.GetEnvironmentVariable("tenant"),
Environment.GetEnvironmentVariable("secret"));
}
public static GraphServiceClient GetGraphClient(string client, string tenant, string secret)
{
var scopes = new string[] { "https://graph.microsoft.com/.default" };
var confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(client).WithTenantId(tenant).WithClientSecret(secret).Build();
var graphServiceClient =
new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
{
// Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
var authResult = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();
// Add the access token in the Authorization header of the API
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
}));
return graphServiceClient;
}
}
public class ODataService
{
protected const string dtformat = "yyyy-MM-dd";
protected string date_from, date_to;
public void FilterByDate(string from, string to)
{
date_from = from;
date_to = to;
}
public void FilterByDate(DateTime? from, DateTime? to)
{
FilterByDate((from.HasValue ? from.Value : DateTime.Now).ToString(dtformat), (to.HasValue ? to.Value : DateTime.Now).ToString(dtformat));
}
}
public class O365GraphService : ODataService
{
public O365GraphService()
{
}
public async Task SendEmailAsync(Message msg)
{
try
{
string clientId = Environment.GetEnvironmentVariable("clientid"),
tenant = Environment.GetEnvironmentVariable("tenant"),
secret = Environment.GetEnvironmentVariable("secret");
var client = GraphClientFactory.GetGraphClient(clientId, tenant, secret);
await client.Users["glenn@test.com"].SendMail(msg, true).Request().PostAsync();
}
catch (Exception ex)
{
Trace.TraceError($"Sending Email : {ex}");
}
}
}
}