/// <summary> /// Get users from a specified domain /// </summary> private void GetADDomainUsers(string strDomain) { try { DirectoryEntry entry = new DirectoryEntry("LDAP://" + strDomain); entry.AuthenticationType = AuthenticationTypes.Secure; DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.SearchScope = SearchScope.Subtree; mySearcher.Filter = ("(&(objectCategory=person)(objectClass=user))"); // Fix: Without this line, only 1000 users were returned // by the search mySearcher.PageSize = 10; //mySearcher.SizeLimit = 0; SortOption sortOption = new SortOption("sAMAccountName", SortDirection.Ascending); mySearcher.Sort = sortOption; lstUsers.Items.Clear(); User objUser = null; DataTable objTable = null; SearchResultCollection UsersColl = mySearcher.FindAll(); foreach (SearchResult resEnt in UsersColl) { try { DirectoryEntry de = resEnt.GetDirectoryEntry(); //Check user string strUser = de.Properties["sAMAccountName"].Value.ToString(); objUser = new User(); objUser.UserDomain = strDomain; objUser.UserLogin = strUser; objTable = objUser.GetUser(); if (objTable.Rows.Count == 0) { lstUsers.Items.Add(strUser); } objTable = null; } catch (Exception ex) { OfficeWorksException.WriteEventLog(ex.Message); } } } catch (Exception ex) { OfficeWorksException.WriteEventLog(ex.Message); } }
/// <summary> /// Get groups from a specified domain /// </summary> private void GetADGroups(string strDomain) { try { System.DirectoryServices.DirectoryEntry entry = new System.DirectoryServices.DirectoryEntry("LDAP://" + strDomain); entry.AuthenticationType = AuthenticationTypes.Secure; System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry); mySearcher.Filter = "(&(objectCategory=group))"; System.DirectoryServices.SortOption sortOption = new System.DirectoryServices.SortOption("name", System.DirectoryServices.SortDirection.Ascending); mySearcher.Sort = sortOption; lstGroups.Items.Clear(); AccessManager objAccess = new AccessManager(); DataTable objTable = objAccess.GetGroups(); objTable.DefaultView.Sort = "GroupDesc"; foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll()) { try { System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry(); // Get group name string strGroupName = de.Properties["name"].Value.ToString(); // Verify if group allready exists on OfficeWorks int RowNum = objTable.DefaultView.Find(strGroupName); // If group does no exists if (RowNum == -1) { lstGroups.Items.Add(strGroupName); } } catch (Exception ex) { OfficeWorksException.WriteEventLog(ex.Message); } } } catch (Exception ex) { OfficeWorksException.WriteEventLog(ex.Message); } }