Beispiel #1
0
        /// <summary>
        /// Performs LDAP Search and extracts attributes.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="currentTime">Locked program timestamp value</param>
        Task ExtractLdapResultsAsync(DateTime currentTime)
        {
            var attributesToAdd = new List <string>();

            foreach (var item in Properties)
            {
                attributesToAdd.Add(item.Mapping);
            }
            attributesToAdd.Add("userAccountControl");
            var ldapFilter    = SetQueryFilter(BatchAction, currentTime);
            var searchRequest = new SearchRequest(SearchRoot, ldapFilter, SearchScope.Subtree, attributesToAdd.ToArray());
            var pageResponse  = new PageResultRequestControl(PageSize);
            var searchOptions = new SearchOptionsControl(System.DirectoryServices.Protocols.SearchOption.DomainScope);

            searchRequest.Controls.Add(pageResponse);
            searchRequest.Controls.Add(searchOptions);
            Log?.LogInformation($"Establishing LDAP Connection to: {ServerName}");
            using (var connection = CreateLdapConnection())
            {
                Log?.LogInformation($"Performing a {BatchAction} operation with filter: {ldapFilter}");
                while (true)
                {
                    SearchResponse response = null;
                    try { response = connection.SendRequest(searchRequest) as SearchResponse; }
                    catch (Exception e) { throw new Exception("An error occurred whilst creating the SearchResponse", e); }

                    var responseCount    = response.Entries.Count;
                    var currentBatchSize = (responseCount != PageSize ? responseCount : PageSize);
                    var filePath         = CsvCreateFile(DirectoryLocation, _totalUsers, currentBatchSize);

                    foreach (var control in response.Controls)
                    {
                        if (control is PageResultResponseControl responseControl)
                        {
                            pageResponse.Cookie = responseControl.Cookie;
                            break;
                        }
                    }

                    // Create CSV file for current batch of users
                    using (var batchFile = new CsvWriter(filePath, Log))
                    {
                        // Create column headings for CSV file
                        var heading = new CsvRow();
                        // Iterate over attribute headings
                        foreach (var item in Properties)
                        {
                            heading.Add(item.Name);
                        }
                        batchFile.CsvWrite(heading);
                        // Create new CSV row for each user
                        foreach (SearchResultEntry sre in response.Entries)
                        {
                            // Placeholder for CSV entry of current user
                            var entry     = new CsvRow();
                            var syncValue = true;

                            // Get whether account is disabled
                            var userAccountControlAttr = sre.Attributes["userAccountControl"];
                            var accountDisabled        = true;
                            if (userAccountControlAttr != null && userAccountControlAttr.Count > 0)
                            {
                                var userAccountControlValue = userAccountControlAttr[0].ToString();
                                try
                                {
                                    var userAccountControl = int.Parse(userAccountControlValue);
                                    accountDisabled = (userAccountControl & ACCOUNTDISABLE) == ACCOUNTDISABLE;
                                }
                                catch (Exception e) { Log?.LogCritical(e, e.Message); }
                            }

                            // Extract each user attribute specified in XML file
                            foreach (var item in Properties)
                            {
                                try
                                {
                                    var attr  = sre.Attributes[item.Mapping];
                                    var value = (attr != null && attr.Count > 0 ? attr[0].ToString() : string.Empty);
                                    if (syncValue && TryParseValue(item, entry, value, accountDisabled, attr, sre))
                                    {
                                        continue;
                                    }
                                    if (item.Index == UserNameIndex)
                                    {
                                        entry.Add(CreateUserAccountName(value, attr));
                                        continue;
                                    }
                                    entry.Add(syncValue ? value : string.Empty);
                                }
                                catch (Exception e) { Log?.LogCritical(e, string.Empty); _totalFailures++; }
                            }
                            // Write current user to CSV file
                            batchFile.CsvWrite(entry);
                            // Increment user count value
                            _totalUsers++;
                        }
                    }
                    Log?.LogInformation($"Successfully extracted {currentBatchSize} users to {filePath} - the total user count is: {_totalUsers}");
                    if (pageResponse.Cookie.Length == 0)
                    {
                        break;
                    }
                }
            }
            return(Task.CompletedTask);
        }
Beispiel #2
0
        /// <summary>
        /// Performs SQL query and extracts attributes.
        /// </summary>
        /// <param name="log">The log.</param>
        /// <param name="currentTime">Locked program timestamp value</param>
        async Task ExtractSqlResultsAsync(DateTime currentTime)
        {
            Log?.LogInformation($"Establishing SQL Connection to: {ConnectionName}");
            using (var connection = CreateSqlConnection())
            {
                Log?.LogInformation($"Performing a sql operation on: {StoredProcedure}");
                List <dynamic> set = null;
                try { set = (await connection.QueryAsync(StoredProcedure, null, commandType: CommandType.StoredProcedure, commandTimeout: CommandTimeout)).ToList(); }
                catch (Exception e) { throw new Exception("An error occurred whilst querying", e); }

                foreach (var items in set.GroupAt(PageSize, x => x))
                {
                    var responseCount    = items.Count();
                    var currentBatchSize = responseCount != PageSize ? responseCount : PageSize;
                    var filePath         = CsvCreateFile(DirectoryLocation, _totalUsers, currentBatchSize);

                    // Create CSV file for current batch of users
                    using (var batchFile = new CsvWriter(filePath, Log))
                    {
                        // Create column headings for CSV file
                        var heading = new CsvRow();
                        // Iterate over attribute headings
                        foreach (var item in Properties)
                        {
                            heading.Add(item.Name);
                        }
                        batchFile.CsvWrite(heading);
                        // Create new CSV row for each user
                        foreach (IDictionary <string, object> sre in items)
                        {
                            // Placeholder for CSV entry of current user
                            var entry = new CsvRow();
                            // Extract each user attribute specified in XML file
                            foreach (var item in Properties)
                            {
                                try
                                {
                                    var value = sre[item.Mapping] != null ? sre[item.Mapping].ToString() : string.Empty;
                                    if (TryParseValue(item, entry, value, sre))
                                    {
                                        continue;
                                    }
                                    if (item.Index == UserNameIndex)
                                    {
                                        entry.Add(CreateUserAccountName(value));
                                        continue;
                                    }
                                    entry.Add(value);
                                }
                                catch (Exception e) { Log?.LogCritical(e, string.Empty); _totalFailures++; }
                            }
                            // Write current user to CSV file
                            batchFile.CsvWrite(entry);
                            // Increment user count value
                            _totalUsers++;
                        }
                        Log?.LogInformation($"Successfully extracted {currentBatchSize} users to {filePath} - the total user count is: {_totalUsers}");
                    }
                }
            }
        }