Exemplo n.º 1
0
        /// <summary>
        /// Method to unlock loaded accounts.  Operates as worker thread.
        /// </summary>
        /// <param name="state">Unused paramter for transmissing state.</param>
        private void UnlockAccounts(object state)
        {
            // Configure access to AD.
            var adService = new AdService(new AdConfiguration {
                Server = this.config.Domain, Username = this.config.Username, Password = this.config.Password, IgnoreServerIpAddresses = this.config.IgnoreServerIpAddresses
            });

            // Loop execution.
            while (!this.stopping.WaitOne(0))
            {
                log.Debug("Beginning unlock cycle.");

                // Unlock accounts.
                foreach (var unlockAccount in this.config.UnlockAccounts)
                {
                    // Wrap into try-catch block so service continues to operate regardless of errors.
                    try
                    {
                        // Attempt to load user.
                        var account = adService.GetUser(unlockAccount);

                        // If account exists, try to unlock it.
                        if (account != null)
                        {
                            // See if account is locked out.
                            if (account.IsAccountLockedOut())
                            {
                                // Unlock account.
                                account.UnlockAccount();
                                log.Info(unlockAccount + " has been unlocked at " + DateTime.Now.ToString());
                                Console.WriteLine(unlockAccount + " has been unlocked at " + DateTime.Now.ToString());
                            }
                            else
                            {
                                log.Debug(unlockAccount + " was already unlocked at " + DateTime.Now.ToString());
                                Console.WriteLine(unlockAccount + " was already unlocked at " + DateTime.Now.ToString());
                            }

                            // Dispose of account object.
                            account.Dispose();
                        }
                    }
                    catch { }
                }

                log.Debug("Ending unlock cycle.  Waiting for next cycle to begin.");

                // Sleep for specified time or signal is received.
                this.stopping.WaitOne(this.config.UnlockFrequency * 1000);
            }

            // Signal processing has ended.
            this.stoppedEvent.Set();
        }