-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathGraphHelper.cs
More file actions
144 lines (124 loc) · 4.37 KB
/
GraphHelper.cs
File metadata and controls
144 lines (124 loc) · 4.37 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using Azure.Core;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Models;
namespace GraphTutorial;
public class GraphHelper
{
// <UserAuthConfigSnippet>
// Settings object
private static Settings? settings;
// User auth token credential
private static DeviceCodeCredential? deviceCodeCredential;
// Client configured with user authentication
private static GraphServiceClient? userClient;
public static void InitializeGraphForUserAuth(
Settings settings,
Func<DeviceCodeInfo, CancellationToken, Task> deviceCodePrompt)
{
GraphHelper.settings = settings;
var options = new DeviceCodeCredentialOptions
{
ClientId = settings.ClientId,
TenantId = settings.TenantId,
DeviceCodeCallback = deviceCodePrompt,
};
deviceCodeCredential = new DeviceCodeCredential(options);
userClient = new GraphServiceClient(deviceCodeCredential, settings.GraphUserScopes);
}
// </UserAuthConfigSnippet>
// <GetUserTokenSnippet>
public static async Task<string> GetUserTokenAsync()
{
// Ensure credential isn't null
_ = deviceCodeCredential ??
throw new NullReferenceException("Graph has not been initialized for user auth");
// Ensure scopes isn't null
_ = settings?.GraphUserScopes ?? throw new ArgumentNullException("Argument 'scopes' cannot be null");
// Request token with given scopes
var context = new TokenRequestContext(settings.GraphUserScopes);
var response = await deviceCodeCredential.GetTokenAsync(context);
return response.Token;
}
// </GetUserTokenSnippet>
// <GetUserSnippet>
public static Task<User?> GetUserAsync()
{
// Ensure client isn't null
_ = userClient ??
throw new NullReferenceException("Graph has not been initialized for user auth");
return userClient.Me.GetAsync((config) =>
{
// Only request specific properties
config.QueryParameters.Select = ["displayName", "mail", "userPrincipalName"];
});
}
// </GetUserSnippet>
// <GetInboxSnippet>
public static Task<MessageCollectionResponse?> GetInboxAsync()
{
// Ensure client isn't null
_ = userClient ??
throw new NullReferenceException("Graph has not been initialized for user auth");
return userClient.Me
.MailFolders["Inbox"] // Only messages from Inbox folder
.Messages
.GetAsync((config) =>
{
/* Only request specific properties */
config.QueryParameters.Select = ["from", "isRead", "receivedDateTime", "subject"];
/* Get at most 25 results */
config.QueryParameters.Top = 25;
/* Sort by received time, newest first */
config.QueryParameters.Orderby = ["receivedDateTime DESC"];
});
}
// </GetInboxSnippet>
// <SendMailSnippet>
public static async Task SendMailAsync(string subject, string body, string recipient)
{
// Ensure client isn't null
_ = userClient ??
throw new NullReferenceException("Graph has not been initialized for user auth");
// Create a new message
var message = new Message
{
Subject = subject,
Body = new ItemBody
{
Content = body,
ContentType = BodyType.Text,
},
ToRecipients =
[
new Recipient
{
EmailAddress = new EmailAddress
{
Address = recipient,
},
},
],
};
// Send the message
await userClient.Me
.SendMail
.PostAsync(new SendMailPostRequestBody
{
Message = message,
});
}
// </SendMailSnippet>
#pragma warning disable CS1998
// <MakeGraphCallSnippet>
/* This function serves as a playground for testing Graph snippets */
/* or other code */
public static async Task MakeGraphCallAsync()
{
// INSERT YOUR CODE HERE
}
// </MakeGraphCallSnippet>
}