public User AddBasicUser(User localUser)
        {
            _logger.Debug("AddBasicUser ");
            User oktaUser = null;

            try
            {
                //oktaUser = _usersClient.Add(user: localUser, activate: true, sendActivaionEmail: false);
                oktaUser = _usersClient.Add(user: localUser, activate: true);
            }
            catch (OktaException ex)
            {
                //log the conditions
                if (ex.ErrorSummary != null)
                {
                    //logger.Error(ex.ErrorSummary.ToString() + ":" + ex.HttpStatusCode.ToString());
                }
                if (ex.ErrorCauses != null)
                {
                    for (int tt = 0; tt < ex.ErrorCauses.Length; tt++)
                    {
                        _logger.Error(ex.ErrorCauses[tt].ErrorSummary.ToString());
                    }
                }
                _logger.Error(ex.ToString());
                //throw new Exception("failed to add user " + localUser.Profile.Login);
                _logger.Error("failed to add user " + oktaUser.Profile.Login);
            }

            return(oktaUser);
        }
Пример #2
0
        public static void CreateUser()
        {
            oktaTenantUrl = ConfigurationManager.AppSettings["OktaTenantUrl"];
            oktaApiKey    = ConfigurationManager.AppSettings["OktaApiKey"];
            oktaUserLogin = ConfigurationManager.AppSettings["NewUserLogin"];
            oktaUserEmail = ConfigurationManager.AppSettings["NewUserEmail"];

            Console.WriteLine("\r\nWelcome to the {0} Okta organization.\r\n", oktaTenantUrl);


            //A valid Api Key IS necessary when using the generic Okta Client (a convenience client to create other clients)
            if (!string.IsNullOrWhiteSpace(oktaApiKey))
            {
                oktaClient = new OktaClient(oktaApiKey, new Uri(oktaTenantUrl));

                //the OktaClient object can be used to instantiate other clients such as the UsersClient object (to manage Okta users)
                usersClient = oktaClient.GetUsersClient();
            }
            try
            {
                if (string.IsNullOrEmpty(oktaUserLogin))
                {
                    Console.Write("Please enter the login of the new user (as an email address) and press Enter: ");
                    oktaUserLogin = Console.ReadLine();
                }

                if (string.IsNullOrEmpty(oktaUserEmail))
                {
                    Console.Write("Please enter the email address for your new user and press Enter: ");
                    oktaUserEmail = Console.ReadLine();
                }

                //create the Okta user
                User newUser = new User(oktaUserLogin, oktaUserEmail, "First Name", "Last Name");

                //this is what you would do to set a custom attribute on the Okta user's profile
                newUser.Profile.SetProperty("employeeNumber", "1234");

                //Activating the user though the API will trigger an email to the user's primary email (i.e. oktaUserEmail)
                usersClient.Add(newUser, true);

                Console.WriteLine("An activation email to {0} is on its way!", oktaUserEmail);
            }
            catch (OktaException oex)
            {
                Console.WriteLine(oex.ErrorCode + ":" + oex.ErrorSummary);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();

                throw;
            }
        }
Пример #3
0
        public void CreateUserAndSetCustomAttributeTest()
        {
            var usersClient = new UsersClient(_token, new Uri(_orgUrl));

            var identifier = DateTime.Now.Ticks.ToString();
            var email      = $"Test_{identifier}@test.com";
            var user       = new User(email, email, $"OktaCoreClient_FN_{identifier}", $"OktaCoreClient_LN{identifier}");

            // Create the user
            user = usersClient.Add(user);

            //Test Retrieve
            var userData = usersClient.Get(email);

            userData.SetProperty("CrmId", identifier);
            userData = usersClient.Update(userData);

            Assert.IsNotNull(usersClient);
        }
Пример #4
0
        protected string mfaWithOkta(string username)
        {
            OktaSettings oktaSettings = new Okta.Core.OktaSettings();

            oktaSettings.ApiToken = "00jSoRPyhdLF9MBypNmvkdm0LEXKZAc4tam7lw1Dqq";
            oktaSettings.BaseUri  = new Uri("https://org.oktapreview.com");

            UsersClient usersClient = new UsersClient(oktaSettings);

            //create with fakedomain, fake email, fake first&last name
            User tempUser = new User(username + "@oktalife.info", "*****@*****.**", "Fake", "Fake");


            tempUser.Credentials = new LoginCredentials();

            String password = System.Convert.ToBase64String(HMACSHA256PasswordGenerator.GenerateHash(username));

            tempUser.Credentials.Password.Value = password;

            string[] groups = new string[1];

            //add the user to the Okta group that prompts for MFA
            groups[0] = "00gbdoy4imvfNeV3Z0h7";
            tempUser.SetProperty("groupIds", groups);
            Console.WriteLine(tempUser.ToJson());

            try
            {
                usersClient.Add(tempUser, true);
            }
            catch (OktaException ex)
            {
                //user already created, move on
            }

            AuthClient   authN    = new Okta.Core.Clients.AuthClient(oktaSettings);
            AuthResponse authResp = authN.Authenticate(username, password);

            return(authResp.StateToken);
        }