-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
265 lines (226 loc) · 11.7 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Configuration;
using System.IO;
namespace B2CGraphShell
{
// This program is a conversion from B2CGraphClient using the ADAL library to B2CGraphClient using the MSAL library
public class Program
{
public static string authority = ConfigurationManager.AppSettings["b2c:Authority"];
public static string tenant = ConfigurationManager.AppSettings["b2c:Tenant"];
public static string clientId = ConfigurationManager.AppSettings["b2c:ClientId"];
public static string clientSecret = ConfigurationManager.AppSettings["b2c:ClientSecret"];
public static string environment = ConfigurationManager.AppSettings["b2c:Environment"];
public static B2CGraphClientMSAL client = null;
public static ConsoleColor init = ConsoleColor.White;
static void Main(string[] args)
{
init = Console.ForegroundColor;
if (args.Length <= 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please enter a command as the first argument. Try 'B2C Help' for a list of commands.");
Console.ForegroundColor = init;
return;
}
client = new B2CGraphClientMSAL(clientId, clientSecret, tenant, authority);
try
{
switch (args[0].ToUpper())
{
case "GET-USER":
GetUser(args);
break;
case "CREATE-USER":
CreateUser(args);
break;
case "UPDATE-USER":
UpdateUser(args);
break;
case "DELETE-USER":
DeleteUser(args);
break;
case "GET-EXTENSION-ATTRIBUTE":
GetExtensionAttribute(args);
break;
case "GET-B2C-APPLICATION":
GetB2CExtensionApplication(args);
break;
case "HELP":
PrintHelp(args);
break;
case "SYNTAX":
PrintSyntax(args);
break;
default:
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid command. Try 'B2C Help' for a list of commands.");
break;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
var innerException = ex.InnerException;
if (innerException != null)
{
while (innerException != null)
{
Console.WriteLine(innerException.Message);
innerException = innerException.InnerException;
}
}
else
{
Console.WriteLine(ex.Message);
}
}
finally
{
Console.ForegroundColor = init;
// Console.ReadLine();
}
}
private static void GetUser(string[] args)
{
Guid temp;
string result;
if (args.Length <= 1)
{
result = client.GetAllUsers(null).Result;
}
else if (Guid.TryParse(args[1], out temp))
{
result = client.GetUserByObjectId(args[1]).Result;
}
else
{
result = client.GetAllUsers(args[1]).Result;
}
object formatted = JsonConvert.DeserializeObject(result);
string returnString = formatted.ToString();
if (returnString.Contains("error"))
Console.ForegroundColor = ConsoleColor.Red;
else
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(JsonConvert.SerializeObject(formatted, Formatting.Indented));
}
private static void CreateUser(string[] args)
{
if (args.Length <= 1)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please include a path to a .json file. Run B2C Syntax to see examples.");
Console.ForegroundColor = init;
return;
}
string json = File.ReadAllText(args[1]);
object formatted = JsonConvert.DeserializeObject(client.CreateUser(json).Result);
string returnString = formatted.ToString();
if (returnString.Contains("error"))
Console.ForegroundColor = ConsoleColor.Red;
else
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(JsonConvert.SerializeObject(formatted, Formatting.Indented));
JObject app = JObject.Parse(formatted.ToString());
string id = (string)app["id"];
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\nB2C user ID is " + id + "\n");
Console.WriteLine("\ne.g. B2C Get-User " + id);
Console.WriteLine("\n B2C Update-User " + id + " path to json file");
Console.WriteLine("\n B2C Delete-User " + id);
}
private static void UpdateUser(string[] args)
{
if (args.Length <= 2)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please include an objectId and a path to a .json file. Run B2C Syntax to see examples.");
Console.ForegroundColor = init;
return;
}
string json = File.ReadAllText(args[2]);
object formatted = JsonConvert.DeserializeObject(client.UpdateUser(args[1], json).Result);
}
private static void DeleteUser(string[] args)
{
if (args.Length <= 1)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please include an objectId. Run B2CGraphShell Syntax to see examples.");
Console.ForegroundColor = init;
return;
}
object formatted = JsonConvert.DeserializeObject(client.DeleteUser(args[1]).Result);
}
private static void GetExtensionAttribute(string[] args)
{
if (args.Length <= 1)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Please include the b2c-extensions-app objectId. Run B2C Syntax to see examples.");
Console.ForegroundColor = init;
return;
}
object formatted = JsonConvert.DeserializeObject(client.GetExtensions(args[1]).Result);
string returnString = formatted.ToString();
if (returnString.Contains("error"))
Console.ForegroundColor = ConsoleColor.Red;
else
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(JsonConvert.SerializeObject(formatted, Formatting.Indented));
}
private static void GetB2CExtensionApplication(string[] args)
{
object formatted = JsonConvert.DeserializeObject(client.GetApplications("$filter=startswith(displayName, 'b2c-extensions-app')").Result);
string returnString = formatted.ToString();
if (returnString.Contains("error"))
Console.ForegroundColor = ConsoleColor.Red;
else
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(JsonConvert.SerializeObject(formatted, Formatting.Indented));
JObject app = JObject.Parse(formatted.ToString());
string id = (string)app["value"][0]["id"];
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("\nB2C application ID is " + id);
Console.WriteLine("\nRun B2C Get-Extension-Attribute " + id);
}
private static void PrintSyntax(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("- Square brackets indicate cmdcmdoptional arguments");
Console.WriteLine("- Curly brackets indicate valid choices for a parameter");
Console.WriteLine("- For information on supported query expressions, including $filter, $top, $orderby, and $expand, see https://docs.microsoft.com/en-us/graph/query-parameters");
Console.WriteLine("- To find the objectId of the b2c-extensions-app, run Get-B2C-Application");
Console.WriteLine("");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Get-User : B2C Get-User [UserObjectId || Query]");
Console.WriteLine(" : B2C Get-User 6d51065f-2e1d-4707-8ec9-ad491bae55dd");
Console.WriteLine("Create-User : B2C Create-User RelativePathToJson");
Console.WriteLine(" : B2C Create-User ..\\..\\..\\usertemplate-email.json");
Console.WriteLine("Update-User : B2C Update-User UserObjectId RelativePathToJson");
Console.WriteLine(" : B2C Update-User 6d51065f-2e1d-4707-8ec9-ad491bae55dd ..\\..\\..\\usertemplate-email.json");
Console.WriteLine("Delete-User : B2C Delete-User UserObjectId");
Console.WriteLine(" : B2C Delete-User 6d51065f-2e1d-4707-8ec9-ad491bae55dd");
Console.WriteLine("Get-Extension-Attribute : B2C Get-Extension-Attribute B2CExtensionsApplicationObjectId");
Console.WriteLine(" : B2C Get-Extension-Attribute 909544d8-f8c0-49c7-b137-a89faff6f882");
Console.WriteLine("Get-B2C-Application : B2C Get-B2C-Application");
Console.WriteLine("Help : B2C Help");
Console.WriteLine("Syntax : B2C Syntax");
}
private static void PrintHelp(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Get-User : Read users from your B2C directory. Optionally accepts an ObjectId as a 2nd argument, and query expression as a 3rd argument.");
Console.WriteLine("Create-User : Create a new user in your B2C directory. Requires a path to a .json file which contains required and optional information as a 2nd argument.");
Console.WriteLine("Update-User : Update an existing user in your B2C directory. Requires an objectId as a 2nd arguemnt & a path to a .json file as a 3rd argument.");
Console.WriteLine("Delete-User : Delete an existing user in your B2C directory. Requires an objectId as a 2nd argument.");
Console.WriteLine("Get-Extension-Attribute : Lists all extension attributes in your B2C directory. Requires the b2c-extensions-app objectId as the 2nd argument.");
Console.WriteLine("Get-B2C-Application : Get the B2C Extensions Application in your B2C directory, so you can retrieve the objectId and pass it to other commands.");
Console.WriteLine("Help : Prints this help menu.");
Console.WriteLine("Syntax : Gives syntax information for each command, along with examples.");
}
}
}