public static async Task ListUsersWithCustomAttribute(GraphServiceClient graphClient, string b2cExtensionAppClientId) { AppSettings config = AppSettingsFile.ReadFromJsonFile(); if (string.IsNullOrWhiteSpace(b2cExtensionAppClientId)) { throw new ArgumentException("B2cExtensionAppClientId (its Application ID) is missing from appsettings.json. Find it in the App registrations pane in the Azure portal. The app registration has the name 'b2c-extensions-app. Do not modify. Used by AADB2C for storing user data.'.", nameof(b2cExtensionAppClientId)); } // Declare the names of the custom attributes var customAttributeName1 = config.ConsultantIdCustomAttributeName; var customAttributeName2 = config.TerritoryNameCustomAttributeName; // Get the complete name of the custom attribute (Azure AD extension) Helpers.B2cCustomAttributeHelper helper = new Helpers.B2cCustomAttributeHelper(b2cExtensionAppClientId); string consultandIdAttrName = helper.GetCompleteAttributeName(customAttributeName1); string territoryNameAttrName = helper.GetCompleteAttributeName(customAttributeName2); Console.WriteLine($"Getting list of users with the custom attributes '{customAttributeName1}' (string) and '{customAttributeName2}' (string)"); Console.WriteLine(); // Get all users (one page) var result = await graphClient.Users .Request() .Select($"id,displayName,identities,{consultandIdAttrName},{territoryNameAttrName}") .GetAsync(); foreach (var user in result.CurrentPage) { Console.WriteLine(JsonConvert.SerializeObject(user)); // Only output the custom attributes... //Console.WriteLine(JsonConvert.SerializeObject(user.AdditionalData)); } }
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); await UserService.ChangeUserSignInName(config, graphClient); }
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(); }
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 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(); }