Exemplo n.º 1
0
        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("");
                }
            }
        }
Exemplo n.º 2
0
        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++;
            }
        }