private static async Task RunAsync() { // Read application settings from appsettings.json (tenant ID, app ID, client secret, etc.) AppSettings config = AppSettingsFile.ReadFromJsonFile(); // Initialize the client credential auth provider IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(config.AppId) .WithTenantId(config.TenantId) .WithClientSecret(config.ClientSecret) .Build(); ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication); // Set up the Microsoft Graph service client with client credentials GraphServiceClient graphClient = new GraphServiceClient(authProvider); Program.PrintCommands(); try { while (true) { Console.Write("Enter command, then press ENTER: "); string decision = Console.ReadLine(); switch (decision.ToLower()) { case "1": await UserService.ListUsers(graphClient);; break; case "2": await UserService.GetUserById(graphClient);; break; case "3": await UserService.GetUserBySignInName(config, graphClient);; break; case "4": await UserService.DeleteUserById(graphClient); break; case "5": await UserService.SetPasswordByUserId(graphClient); break; case "6": await UserService.BulkCreate(config, graphClient); break; case "7": await UserService.ListApplications(graphClient); break; case "help": Program.PrintCommands(); break; case "exit": return; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid command. Enter 'help' to show a list of commands."); Console.ResetColor(); break; } Console.ResetColor(); } } 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.ResetColor(); } Console.ReadLine(); }
static async Task Main(string[] args) { // Read application settings from appsettings.json (tenant ID, app ID, client secret, etc.) AppSettings config = AppSettingsFile.ReadFromJsonFile(); // Initialize the client credential auth provider var scopes = new[] { "https://graph.microsoft.com/.default" }; var clientSecretCredential = new ClientSecretCredential(config.TenantId, config.AppId, config.ClientSecret); var graphClient = new GraphServiceClient(clientSecretCredential, scopes); PrintCommands(); try { while (true) { Console.Write("Enter command, then press ENTER: "); string decision = Console.ReadLine(); switch (decision.ToLower()) { case "1": await UserService.ListUsers(graphClient); break; case "2": await UserService.GetUserById(graphClient); break; case "3": await UserService.GetUserBySignInName(config, graphClient); break; case "4": await UserService.DeleteUserById(graphClient); break; case "5": await UserService.SetPasswordByUserId(graphClient); break; case "6": await UserService.BulkCreate(config, graphClient); break; case "7": await UserService.CreateUserWithCustomAttribute(graphClient, config.B2cExtensionAppClientId, config.TenantId); break; case "8": await UserService.ListUsersWithCustomAttribute(graphClient, config.B2cExtensionAppClientId); break; case "9": await UserService.CountUsers(graphClient); break; case "help": Program.PrintCommands(); break; case "exit": return; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid command. Enter 'help' to show a list of commands."); Console.ResetColor(); break; } Console.ResetColor(); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"An error occurred: {ex}"); Console.ResetColor(); } Console.ReadLine(); }
static async Task Main(string[] args) { // Read application settings from appsettings.json (tenant ID, app ID, client secret, etc.) AppSettings config = AppSettingsFile.ReadFromJsonFile(); // Initialize the client credential auth provider IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder .Create(config.AppId) .WithTenantId(config.TenantId) .WithClientSecret(config.ClientSecret) .Build(); ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication); // Set up the Microsoft Graph service client with client credentials GraphServiceClient graphClient = new GraphServiceClient(authProvider); // Create object to store create user responses List <SpreadsheetModelUserCreated> responses = new List <SpreadsheetModelUserCreated>(); PrintCommands(); try { while (true) { Console.Write("Enter command, then press ENTER: "); string decision = Console.ReadLine(); switch (decision.ToLower()) { case "1": await UserService.ListUsers(graphClient); break; case "2": await UserService.GetUserById(graphClient); break; case "3": await UserService.GetUserBySignInName(config, graphClient); break; case "4": await UserService.DeleteUserById(graphClient); break; case "5": await UserService.SetPasswordByUserId(graphClient); break; case "6": await UserService.BulkCreate(config, graphClient); break; case "7": await UserService.CreateUserWithCustomAttribute(graphClient, config.B2cExtensionAppClientId, config.TenantId); break; case "8": await UserService.ListUsersWithCustomAttribute(graphClient, config.B2cExtensionAppClientId); break; case "9": await UserService.BulkCreateFromCsv(config, graphClient, responses); SpreadsheetService.SaveResponseDataAsCSV(responses); break; case "help": Program.PrintCommands(); break; case "exit": return; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid command. Enter 'help' to show a list of commands."); Console.ResetColor(); break; } Console.ResetColor(); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"An error occurred: {ex}"); Console.ResetColor(); } Console.ReadLine(); }