-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cs
211 lines (188 loc) · 7.31 KB
/
common.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Graph;
using Microsoft.Graph.Auth;
using System.Configuration;
using System.Collections;
using System.Windows.Forms;
namespace OneDriveToolls
{
static class TokenCacheHelper
{
/// <summary>
/// Path to the token cache
/// </summary>
//public static readonly string CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalcache.bin3";
public static void EnableSerialization(ITokenCache tokenCache)
{
tokenCache.SetBeforeAccess(BeforeAccessNotification);
tokenCache.SetAfterAccess(AfterAccessNotification);
}
private static readonly string CacheFilePath = "msalcache.bin";
private static readonly object FileLock = new object();
private static void BeforeAccessNotification(TokenCacheNotificationArgs args)
{
lock (FileLock)
{
args.TokenCache.DeserializeMsalV3(System.IO.File.Exists(CacheFilePath)
? ProtectedData.Unprotect(System.IO.File.ReadAllBytes(CacheFilePath),
null,
DataProtectionScope.CurrentUser)
: null);
}
}
private static void AfterAccessNotification(TokenCacheNotificationArgs args)
{
// if the access operation resulted in a cache update
if (args.HasStateChanged)
{
lock (FileLock)
{
// reflect changesgs in the persistent store
System.IO.File.WriteAllBytes(CacheFilePath,
ProtectedData.Protect(args.TokenCache.SerializeMsalV3(),
null,
DataProtectionScope.CurrentUser)
);
}
}
}
}
class ConfigSet {
const string DEFUALT_KEY = "defaultlogin";
public string ClientID { get; }
public List<string> Users { get; }
private Configuration conf;
public ConfigSet(string clientid)
{
this.ClientID = clientid;
this.conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
private bool HasClientID()
{
if (conf.AppSettings.Settings.AllKeys.Where(a => a == this.ClientID).Any())
{
return true;
}
else
{
return false;
}
}
public bool HasUser(string username)
{
this.conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
string userList = conf.AppSettings.Settings[this.ClientID].Value;
if (!this.HasClientID()) {
return false;
}
if (userList.Split(',').Where(a => a == username).Any())
{
return true;
}
else {
return false;
}
}
public void AddUser(string username, bool defualtlogin=false) {
}
}
class DataRequest
{
private IPublicClientApplication app;
private string userName;
private string cliendID;
private string[] scopes;
private string baseEndpoint;
public string UserName { get => userName; private set => userName = value; }
public string CliendID { get => cliendID; private set => cliendID = value; }
public string[] Scopes { get => scopes; private set => scopes = value; }
public string BaseEndpoint { get => baseEndpoint; set => baseEndpoint = value; }
public DataRequest(string clientid, string username, string[] scopes)
{
this.userName = username;
this.cliendID = clientid;
this.scopes = scopes;
this.app = PublicClientApplicationBuilder.Create(clientid)
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
.WithAuthority(AzureCloudInstance.AzurePublic, "common")
.Build();
app = PublicClientApplicationBuilder.Create(clientid)
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
.Build();
}
public DataRequest(string clientid, string username) : this(clientid, username, new string[] { "files.read", "Files.ReadWrite" })
{
}
public void EnableCache(string cacheFileName) {
TokenCacheHelper.EnableSerialization(this.app.UserTokenCache);
}
public async Task<string> GetTokenAsync()
{
AuthenticationResult authResult = null;
var accounts = await this.app.GetAccountsAsync();
try
{
authResult = await this.app.AcquireTokenSilent(this.scopes, accounts.Where(o => o.Username == this.userName).FirstOrDefault())
.ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
try
{
authResult = await this.app.AcquireTokenInteractive(this.scopes)
.WithAccount(accounts.FirstOrDefault())
.WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
.ExecuteAsync();
}
catch (MsalException msalex)
{
throw msalex;
}
}
catch (Exception ex)
{
throw ex;
}
if (authResult != null)
{
return authResult.AccessToken;
}
else
{
return "";
}
}
public async Task<string> GetHttpContentAsync(string endpoint)
{
var httpClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response;
try
{
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, endpoint);
string token = await this.GetTokenAsync();
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
return content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
public async Task<User> GetRootDriveAsync()
{
InteractiveAuthenticationProvider authProvider = new InteractiveAuthenticationProvider(this.app,this.scopes);
var graphClient = new GraphServiceClient(authProvider);
return await graphClient.Me.Request().GetAsync();
}
}
}