-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathMsGraphUserProvider.cs
87 lines (75 loc) · 3.27 KB
/
MsGraphUserProvider.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Azure.Core;
using DWSIM.Simulate365.Enums;
using DWSIM.Simulate365.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace DWSIM.Simulate365
{
public class MsGraphUserProvider : IUserProvider
{
const string TENANT_ID = "eb2542b8-5a5d-4f61-a9b5-6ce7dbc4ebfd";
const string CLIENT_ID = "d18e5f18-7709-4ef0-913e-3c8eeecd7d60";
const string SCOPE = "profile openid offline_access";
const string RETURN_URL = "https://dwsim-login-return.simulate365.com";
public async Task<UserDetailsModel> GetUserDetailsAsync(string accessToken)
{
var graphClient = GraphClientFactory.CreateClient(accessToken);
var user = await graphClient.Me.Request().GetAsync();
var currentUser = new UserDetailsModel
{
DisplayName = $"{user.GivenName} {user.Surname}",
FirstName = user.GivenName,
LastName = user.Surname,
Id = user.Id,
UserPrincipalName = user.UserPrincipalName
};
return currentUser;
}
public async Task<OAuthTokenResponse> RefreshTokenAsync(string refreshToken)
{
try
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string refreshUrl = $"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token";
var formData = new Dictionary<string, string>()
{
["client_id"] = CLIENT_ID,
["refresh_token"] = refreshToken,
["scope"] = SCOPE,
["redirect_uri"] = RETURN_URL,
["grant_type"] = "refresh_token",
};
HttpResponseMessage response = await client.PostAsync(refreshUrl, new FormUrlEncodedContent(formData));
var responseStr = await response.Content.ReadAsStringAsync();
// Check for error
var errorResponse = JsonConvert.DeserializeObject<OAuthErrorResponse>(responseStr);
if (errorResponse != null && !String.IsNullOrWhiteSpace(errorResponse.Error))
{
var errorMessage = "An error occured while refreshing authorizaton token.";
if (!String.IsNullOrWhiteSpace(errorResponse.ErrorDescription))
errorMessage = errorResponse.ErrorDescription;
throw new Exception(errorMessage);
}
// Deserialize
var token = JsonConvert.DeserializeObject<OAuthTokenResponse>(responseStr);
token.AccessTokenType = AccessTokenType.MsGraph;
return token;
}
}
catch (Exception ex)
{
// UserLoggedOut?.Invoke(this, new EventArgs());
return null;
}
}
}
}