public async Task Create100APIMUsers(UserNormalization un) { UserContract userContractbase = GetUserContractFor(un.Email); string firstName = userContractbase.Properties.FirstName; string email = userContractbase.Properties.Email; UserContract userContract = userContractbase; for (int iter100 = 0; iter100 < 100; iter100++) { userContract.Properties.FirstName = firstName + iter100; userContract.Properties.Email = email.Replace("@", iter100 + "@"); userContract.Properties.Identities[0].Id = userContract.Properties.Email; foreach (UserNormalizationStatus uns in un.UsersStatus) { Console.WriteLine(" Creating user in service: " + uns.APIMName); APIMService apimClient = apims[0]; string response = await apimClient.CreateUserInApim(userContract); Console.WriteLine(" Result: " + response.Substring(0, 30) + (response.Length > 30 ? "..." : "")); Console.WriteLine(""); } } }
public MigrationService() { var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config")); adB2CUsersAll = new List <User>(); config = AppSettingsFile.ReadFromJsonFile(); var subscriptions = config.APIMSubscriptionIds == null ? new String[] { } : config.APIMSubscriptionIds.Split(";"); var serviceNames = config.APIMApiManagementNames == null ? new String[] { } : config.APIMApiManagementNames.Split(";"); var resourceGroups = config.APIMResourceGroups == null ? new String[] { } : config.APIMResourceGroups.Split(";"); if (subscriptions.Length > 0 && subscriptions.Length.Equals(serviceNames.Length) && subscriptions.Length.Equals(resourceGroups.Length)) { apims = new APIMService[serviceNames.Length]; apimUserCollections = new UserCollection[serviceNames.Length]; for (int i = 0; i < serviceNames.Length; i++) { apims[i] = new APIMService(serviceNames[i], resourceGroups[i], config.APIMTenantId, subscriptions[i]); } } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error on Configuration:"); Console.WriteLine(" API Mgmt Services : " + serviceNames.Length); Console.WriteLine(" API Mgmt RGs : " + resourceGroups.Length); Console.WriteLine(" API Mgmt Subscrpitions : " + subscriptions.Length); Console.ForegroundColor = ConsoleColor.White; } }
public async Task RestoreAPIM(string backupName) { APIMService apimClient = apims[0]; apimClient.RestoreApimAsync(backupName); Console.WriteLine("APIM Restored"); Console.WriteLine(""); }
public async Task BackupAPIM() { APIMService apimClient = apims[0]; apimClient.BackupApimAsync(); Console.WriteLine("APIM Backed Up"); Console.WriteLine(""); }
private async Task CreateUsersInAzureB2CAndSyncObjectId(UserCollection apimUsersToMigrateToB2C) { APIMService apimClient = apims[0]; //If we have users with AADB2C identity that do not exists on B2C then create them: if (apimUsersToMigrateToB2C != null && apimUsersToMigrateToB2C.value != null) { foreach (var userToMigrate in apimUsersToMigrateToB2C.value) { Console.WriteLine(" [ADB2C] - Creating User: "******" [ADB2C] - User Created: " + addedUser.DisplayName); } Console.WriteLine(" [APIM] - Updating Object: " + userToMigrate.Properties.Email + ", " + addedUser.Id); string result = await apimClient.UpdateUserObjectIdAsync(userToMigrate, addedUser.Id); Console.WriteLine(" [APIM] - Object Id update result: " + result); } } }
public async Task NormalizeUserAsync(UserNormalization un) { if (un.IsNormilized) { log.Info("User already normalized"); return; } Console.WriteLine(""); Console.WriteLine("Starting user normalization..."); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(""); log.Info(" 1) Create user in all APIMs"); int iter1 = 0; UserContract userContract = GetUserContractFor(un.Email); if (userContract != null && !IsAzureIdentity(userContract)) { bool userCreated = false; foreach (UserNormalizationStatus uns in un.UsersStatus) { if (!uns.ExistsInAPIM) { log.Info(" Creating user in service: " + uns.APIMName); APIMService apimClient = apims[iter1]; string response = await apimClient.CreateUserInApim(userContract); log.Info(" Result: " + response.Substring(0, 30) + (response.Length > 30 ? "..." : "")); Console.WriteLine(""); userCreated = true; } iter1++; } if (userCreated) { await RePopulateAPIMUserCollection(); } } else { Console.WriteLine(" User " + userContract.Properties.Email + " not suitable for APIM addition as is an Azure user type"); } Console.WriteLine(" 2) Create user in ADB2C if needed"); int iter2 = 0; string objectId = string.Empty; //ALB - next 2 lines to copy the source APIM to the userContract object UserNormalizationStatus apimFromUNS = (UserNormalizationStatus)un.UsersStatus[0]; userContract.sourceAPIM = apimFromUNS.APIMName; var b2CUser = GetUserInAzureB2CWithEmail(adB2CUsersAll, un.Email); if (b2CUser == null) { if (userContract != null && !IsAzureIdentity(userContract)) { Console.WriteLine(" Creating user ADB2C: " + userContract.Properties.Email); b2CUser = await CreateUserInAzureB2C(userContract); if (b2CUser != null) { Console.WriteLine(" User Created: " + b2CUser.DisplayName + " with Object Id " + b2CUser.Id); } } } Console.WriteLine(" 3) Update Users Identities in APIM"); foreach (UserNormalizationStatus uns in un.UsersStatus) { if (userContract != null && !IsAzureIdentity(userContract)) { if (b2CUser != null) { APIMService apimClient = apims[iter2]; Console.WriteLine(" Updating Object Id in API Management: " + userContract.Properties.Email); var user = GetUserContractFor(uns.APIMName, un.Email); if (user != null) { string result = await apimClient.UpdateUserObjectIdAsync(user, b2CUser.Id); int idx = result == null ? 0 : (result.Length <= 30 ? result.Length : 30); Console.WriteLine(" User Update result: " + result.Substring(0, idx) + (result.Length > 30 ? "..." : "")); } } else { if (config.MigrationEnabled) { Console.WriteLine(" ERROR While creating the user in B2C"); } else { Console.WriteLine(" Skipped updating APIM identities as Migration is not enabled."); } } } else { Console.WriteLine(" User " + userContract.Name + " not suitable for normalization"); } iter2++; } }
private async Task PrintListOfUsersAsync(UserCollection apimUsers, ConsoleColor color) { int i = 1; if (apimUsers != null && apimUsers.value != null) { foreach (var user in apimUsers.value) { Console.ForegroundColor = color; string output = i + "- " + user.Name + "; " + user.Properties.FirstName + "; " + user.Properties.LastName + "; " + user.Properties.State; if (user.Properties.Identities != null) { foreach (var identity in user.Properties.Identities) { output += "; " + identity.Provider + "; " + identity.Id; Console.WriteLine(output); } } i++; } } Console.ResetColor(); Console.WriteLine(); Console.WriteLine("Command Description"); Console.WriteLine("===================="); Console.WriteLine("[#] - To Migrate User / Update Object Id"); Console.WriteLine("[X] - To go back to main menu"); Console.WriteLine("-------------------------"); Console.WriteLine(); string idx = Console.ReadLine().ToUpper(); if (!idx.Equals("X")) { int iIdx = int.Parse(idx); if (iIdx > 0 && iIdx < apimUsers.value.Length + 1) { Console.ResetColor(); Console.WriteLine(); Console.WriteLine("Command Description"); Console.WriteLine("===================="); Console.WriteLine("[M] - To Migrate User"); Console.WriteLine("[U] - Update Object Id"); Console.WriteLine("[X] - To go back to main menu"); Console.WriteLine("-------------------------"); Console.WriteLine(); string idx2 = Console.ReadLine().ToUpper(); if (idx2.Equals("M")) { APIMService apimClient = apims[0]; Console.WriteLine("Will migrate user: "******" - " + apimUsers.value[iIdx - 1].Properties.FirstName); var addedUser = await CreateUserInAzureB2C(apimUsers.value[iIdx - 1]); Console.WriteLine(" User Recently Added: " + addedUser.DisplayName); await apimClient.UpdateUserObjectIdAsync(apimUsers.value[iIdx - 1], addedUser.Id); } else if (idx2.Equals("U")) { APIMService apimClient = apims[0]; Console.WriteLine("Will Update Object Id for user: "******" - " + apimUsers.value[iIdx - 1].Properties.FirstName); var b2CUser = GetUserInAzureB2CWithEmail(adB2CUsersAll, apimUsers.value[iIdx - 1].Properties.Email); await apimClient.UpdateUserObjectIdAsync(apimUsers.value[iIdx - 1], b2CUser.Id); } } } }