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++; } }
public async Task SetupUserCollections() { //userNormalizationList.Clear(); int iterIdx = 0; foreach (var userCollection in apimUserCollections) { //ALB:-Addition to skip null collections if (userCollection != null) { foreach (var user in userCollection.value) { bool hasADB2C = false; bool isFoundInADB2C = false; bool isEmailFoundInADB2C = false; string objectId = string.Empty; string email = user.Properties.Email; var uns = new UserNormalizationStatus(); foreach (var identity in user.Properties.Identities) { if (identity.Provider.Equals("AadB2C")) { hasADB2C = true; var b2cUser = await GetAADB2CUserById(identity.Id, user.Name); if (b2cUser == null) { isFoundInADB2C = false; } else if (DoesB2CUserHasThisEmail(b2cUser, user.Properties.Email)) { isFoundInADB2C = true; isEmailFoundInADB2C = true; objectId = identity.Id; } else { isFoundInADB2C = true; isEmailFoundInADB2C = false; } break; } } uns.APIMName = ""; uns.HasADB2C = hasADB2C; uns.IsEmailFoundInADB2C = isEmailFoundInADB2C; uns.IsFoundInADB2C = isFoundInADB2C; uns.ObjectId = objectId; uns.ExistsInAPIM = true; if (iterIdx < apims.Length) { uns.APIMName = apims[iterIdx].APIMServiceName; } UserNormalization un = FindUserNormalizationByEmail(userNormalizationList, email); if (un != null) { un.AddUserNormalizationStatus(uns); } else { un = new UserNormalization(); un.Email = email; un.AddUserNormalizationStatus(uns); userNormalizationList.Add(un); } } } else { Console.WriteLine("Empty APIM"); } iterIdx++; } userSetup = true; }