This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathProgram.cs
219 lines (209 loc) · 9.4 KB
/
Program.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
212
213
214
215
216
217
218
219
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json.Linq;
using System;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace DirSearcherClient
{
public class Program
{
public const string resource = "https://graph.microsoft.com";
public const string clientId = "f5b5b9c9-c68b-45c5-8f57-bcf3f2f15f26";
public static void Main(string[] args)
{
string commandString = string.Empty;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("***********************************************************");
Console.WriteLine("* Directory Searcher Text Client *");
Console.WriteLine("* *");
Console.WriteLine("* Type commands to search users *");
Console.WriteLine("* *");
Console.WriteLine("***********************************************************");
Console.WriteLine("");
// main command cycle
while (!commandString.Equals("Exit"))
{
Console.ResetColor();
Console.WriteLine("Enter command (search <upn> <tenantname> | clear | printcache | exit | help) >");
commandString = Console.ReadLine();
switch (commandString.ToUpper())
{
case "CLEAR":
ClearCache();
break;
case "HELP":
Help();
break;
case "PRINTCACHE":
PrintCache();
break;
case "EXIT":
Console.WriteLine("Bye!");
return;
default:
if (commandString.ToUpper().StartsWith("SEARCH") && commandString.Split(' ').Count() > 1)
{
string[] localArgs = commandString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (localArgs.Count() > 2)
Search(localArgs[1], localArgs[2]).Wait();
else
Search(localArgs[1], null).Wait();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid command.");
}
break;
}
}
}
static async Task Search(string searchterm, string tenant)
{
AuthenticationResult ar = await GetToken(tenant);
if (ar != null)
{
JObject jResult = null;
try
{
string graphRequest = $"{resource}/v1.0/users?$filter=mailNickname eq '{searchterm}'";
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, graphRequest);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", ar.AccessToken);
HttpResponseMessage response = client.SendAsync(request).Result;
string content = response.Content.ReadAsStringAsync().Result;
jResult = JObject.Parse(content);
}
catch (Exception ee)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error on search");
Console.WriteLine(ee.Message);
return;
}
if (jResult == null || jResult["odata.error"] != null || jResult["value"] == null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error on search");
}
Console.ForegroundColor = ConsoleColor.Green;
if (jResult != null)
{
if (jResult.Count == 0)
{
Console.WriteLine("No user with alias {0} found. (tenantID: {1})", searchterm, ar.TenantId);
}
else
{
Console.WriteLine("Users found.");
foreach (JToken result in jResult["value"])
{
Console.WriteLine("-----");
Console.WriteLine("displayName: {0}", (string)result["displayName"]);
Console.WriteLine("givenName: {0}", (string)result["givenName"]);
Console.WriteLine("surname: {0}", (string)result["surname"]);
Console.WriteLine("userPrincipalName: {0}", (string)result["userPrincipalName"]);
Console.WriteLine("telephoneNumber: {0}", (string)result["telephoneNumber"] ?? "Not Listed.");
}
}
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Failed to obtain a token.");
}
}
static void PrintCache()
{
AuthenticationContext ctx = new AuthenticationContext("https://login.microsoftonline.com/common");
var cacheContent = ctx.TokenCache.ReadItems();
Console.ForegroundColor = ConsoleColor.Green;
if (cacheContent.Any())
{
Console.WriteLine("{0,-30} | {1,-15}", "UPN", "TenantId");
Console.WriteLine("-----------------------------------------------------------------");
foreach (TokenCacheItem tci in cacheContent)
{
Console.WriteLine("{0,-30} | {1,-15} ", tci.DisplayableId, tci.TenantId);
}
Console.WriteLine("-----------------------------------------------------------------");
}
else { Console.WriteLine("The cache is empty."); }
}
static void ClearCache()
{
AuthenticationContext ctx = new AuthenticationContext("https://login.microsoftonline.com/common");
ctx.TokenCache.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Token cache cleared.");
}
static void Help()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("SEARCH - searches for a user by alias. Requires sign in. \nUsage: \nsearch <useralias> [domain to search]");
Console.WriteLine("CLEAR - empties the token cache, allowing you to sign in as a different user");
Console.WriteLine("PRINTCACHE - displays the current cache content (basic fields only)");
Console.WriteLine("HELP - displays this page");
Console.WriteLine("EXIT - closes this program");
Console.WriteLine("");
}
static async Task<AuthenticationResult> GetToken(string tenant)
{
AuthenticationContext ctx = null;
if (tenant != null)
ctx = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);
else
{
ctx = new AuthenticationContext("https://login.microsoftonline.com/common");
if (ctx.TokenCache.Count > 0)
{
string homeTenant = ctx.TokenCache.ReadItems().First().TenantId;
ctx = new AuthenticationContext("https://login.microsoftonline.com/" + homeTenant);
}
}
AuthenticationResult result = null;
try
{
result = await ctx.AcquireTokenSilentAsync(resource, clientId);
}
catch (AdalSilentTokenAcquisitionException)
{
result = await GetTokenViaCode(ctx);
}
catch (AdalException exc)
{
PrintError(exc);
}
return result;
}
private static void PrintError(Exception exc)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong.");
Console.WriteLine("Message: " + exc.Message + "\n");
}
static async Task<AuthenticationResult> GetTokenViaCode(AuthenticationContext ctx)
{
AuthenticationResult result = null;
try
{
DeviceCodeResult codeResult = await ctx.AcquireDeviceCodeAsync(resource, clientId);
Console.ResetColor();
Console.WriteLine("You need to sign in.");
Console.WriteLine("Message: " + codeResult.Message + "\n");
result = await ctx.AcquireTokenByDeviceCodeAsync(codeResult);
}
catch (Exception exc)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Something went wrong.");
Console.WriteLine("Message: " + exc.Message + "\n");
}
return result;
}
}
}