-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMGraphServices.cs
58 lines (41 loc) · 2.13 KB
/
MGraphServices.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
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace Simplisys_OAuth2_Service
{
class MGraphServices
{
[Obsolete]
public async Task<GraphServiceClient> connectToMailBox(string clientID, string tenantID, string emailAddress, string emailPassword)
{
GraphServiceClient graphServiceClient = await CreateGraphClientService(new PublicClientApplicationOptions
{
ClientId = clientID,
TenantId = tenantID
}, emailAddress, emailPassword);
return graphServiceClient;
}
[Obsolete]
private async Task<GraphServiceClient> CreateGraphClientService(PublicClientApplicationOptions _PublicClientApplicationOptions, string _EmailId, string _Password)
{
try
{
string[] scopes = new string[] { "user.read" };
var pca = PublicClientApplicationBuilder.CreateWithApplicationOptions(_PublicClientApplicationOptions).WithAuthority(AzureCloudInstance.AzurePublic, _PublicClientApplicationOptions.TenantId).Build();
var authResult = await pca.AcquireTokenByUsernamePassword(new string[] { "https://graph.microsoft.com/.default" }, _EmailId, new NetworkCredential("", _Password).SecurePassword).ExecuteAsync();
return new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); }));
//return new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); return Task.CompletedTask; }));
}
catch (Exception ex)
{
string exc = ex.Message.ToString();
throw;
}
}
}
}