Exemplo n.º 1
0
        private async void NonBlockingDeleteUser(BlockingCollection <JObject> bc, CancellationToken ct)
        {
            var threadID = Thread.CurrentThread.ManagedThreadId;

            while (!bc.IsCompleted && !ct.IsCancellationRequested)
            {
                var configuration = new Okta.Sdk.Configuration.OktaClientConfiguration
                {
                    OktaDomain        = oktaOrgURL,
                    Token             = oktaAPIKey,
                    MaxRetries        = 10,
                    ConnectionTimeout = 5,
                    RequestTimeout    = 120
                };
                //var client = new OktaClient(config);

                var httpClient     = new System.Net.Http.HttpClient();
                var maxRetries     = configuration.MaxRetries ?? OktaClientConfiguration.DefaultMaxRetries;
                var requestTimeout = configuration.RequestTimeout ?? OktaClientConfiguration.DefaultRequestTimeout;

                var client = new OktaClient(apiClientConfiguration: configuration, httpClient: httpClient, logger: this.logger, retryStrategy: new CustomRetryStrategy(maxRetries, requestTimeout, 1, logger));

                JObject user = null;
                try
                {
                    user = bc.Take();
                }
                catch (InvalidOperationException)
                {
                }
                if (user != null)
                {
                    //process
                    var uid = user.Value <JToken>("profile").Value <string>("login");
                    threadID = Thread.CurrentThread.ManagedThreadId;
                    //Console.WriteLine($"ThreadID: {threadID}, User: {uid}");
                    this.Invoke((MethodInvoker) delegate()
                    {
                        txtBoxSuccess.AppendText($"ThreadID: {threadID}, Login: {uid}, Request: DeleteUser, User: {uid}{Environment.NewLine}");
                    });

                    try
                    {
                        // Get User
                        var u = await client.Users.GetUserAsync(uid);

                        // First, deactivate the user
                        await u.DeactivateAsync();

                        // Then delete the user
                        await u.DeactivateOrDeleteAsync();

                        threadID = Thread.CurrentThread.ManagedThreadId;
                        this.Invoke((MethodInvoker) delegate()
                        {
                            txtBoxSuccess.AppendText($"ThreadID: {threadID}, Login: {uid}, Response: DeleteUser, User: {uid}, Status: DELETED{Environment.NewLine}");
                        });
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        threadID = Thread.CurrentThread.ManagedThreadId;
                        this.Invoke((MethodInvoker) delegate()
                        {
                            txtBoxFailure.AppendText($"ThreadID: { threadID}, Login: { uid}, Exception: {ex.Message}{Environment.NewLine}");
                        });
                    }

                    Interlocked.Increment(ref userProcessedCount);
                }
                //Thread.SpinWait(5000000);
            }
            threadID = Thread.CurrentThread.ManagedThreadId;
            this.Invoke((MethodInvoker) delegate()
            {
                txtBoxSuccess.AppendText($"ThreadID: {threadID}, Processing Done!{Environment.NewLine}");
            });
        }
Exemplo n.º 2
0
        private async void NonBlockingCreateUser(BlockingCollection <JObject> bc, CancellationToken ct)
        {
            var threadID = Thread.CurrentThread.ManagedThreadId;

            while (!bc.IsCompleted && !ct.IsCancellationRequested)
            {
                var configuration = new Okta.Sdk.Configuration.OktaClientConfiguration
                {
                    OktaDomain        = oktaOrgURL,
                    Token             = oktaAPIKey,
                    MaxRetries        = 10,
                    ConnectionTimeout = 5,
                    RequestTimeout    = 120
                };
                //var client = new OktaClient(config);

                var httpClient     = new System.Net.Http.HttpClient();
                var maxRetries     = configuration.MaxRetries ?? OktaClientConfiguration.DefaultMaxRetries;
                var requestTimeout = configuration.RequestTimeout ?? OktaClientConfiguration.DefaultRequestTimeout;

                var client = new OktaClient(apiClientConfiguration: configuration, httpClient: httpClient, logger: this.logger, retryStrategy: new CustomRetryStrategy(maxRetries, requestTimeout, 1, logger));

                JObject user = null;
                try
                {
                    user = bc.Take();
                }
                catch (InvalidOperationException)
                {
                }
                if (user != null)
                {
                    //process
                    var uid = user.Value <JToken>("profile").Value <string>("login");
                    threadID = Thread.CurrentThread.ManagedThreadId;
                    //Console.WriteLine($"ThreadID: {threadID}, User: {uid}");
                    this.Invoke((MethodInvoker) delegate()
                    {
                        txtBoxSuccess.AppendText($"ThreadID: {threadID}, Request: CreateUser, User: {uid}{Environment.NewLine}");
                    });

                    try
                    {
                        JObject credentials = (JObject)user["credentials"];
                        JObject password    = (JObject)credentials["password"];
                        password["value"] = "a@Z1" + Guid.NewGuid().ToString().Substring(0, 6);

                        var result = await client.PostAsync <User>(new HttpRequest
                        {
                            Uri             = $"/api/v1/users",
                            QueryParameters = new Dictionary <string, object>()
                            {
                                ["activate"] = true,
                            },
                            Payload = user
                        });

                        threadID = Thread.CurrentThread.ManagedThreadId;
                        //Console.WriteLine($"ThreadID: {threadID}, User: {uid}, Status: {result.Status}");
                        this.Invoke((MethodInvoker) delegate()
                        {
                            if (result.Status == "ACTIVE")
                            {
                                txtBoxSuccess.AppendText($"ThreadID: {threadID}, Login: {uid}, Response: CreateUser, User: {uid}, Status: {result.Status}{Environment.NewLine}");
                            }
                            else
                            {
                                txtBoxFailure.AppendText($"ThreadID: {threadID}, Login: {uid}, Response: CreateUser, User: {uid}, Status: {result.Status}{Environment.NewLine}");
                            }
                        });
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        threadID = Thread.CurrentThread.ManagedThreadId;
                        this.Invoke((MethodInvoker) delegate()
                        {
                            txtBoxFailure.AppendText($"ThreadID: { threadID}, Login: { uid}, Exception: {ex.Message}{Environment.NewLine}");
                        });
                    }

                    Interlocked.Increment(ref userProcessedCount);
                }
                //Thread.SpinWait(5000000);
            }
            threadID = Thread.CurrentThread.ManagedThreadId;
            this.Invoke((MethodInvoker) delegate()
            {
                txtBoxSuccess.AppendText($"ThreadID: {threadID}, Processing Done!{Environment.NewLine}");
            });
        }