LDAP Directory Searches

G

Guest

Hi,

I've written sone C# classes to search the active directory for users - see
code below.

The code works fine but is very slow when I'm iterating through the
directory and getting the attributes via the Properties collection.

Extracting over 500 objects is OK but it takes an extra 15 seconds when
accesing the data via the Properties["prop name"].value method.

Does anyone know a quicker method - if there is none I'm going to use
remoting to pre load the information but would be interested to know.

Regards

Mike

public void GetUsers(string domainController)
{
DirectorySearcher directorySearcher = new DirectorySearcher(
new DirectoryEntry("LDAP://" + domainController),
"(&(objectCategory=Person)(objectClass=user))");

SearchResultCollection entries = directorySearcher.FindAll();

foreach (SearchResult result in entries)
{
DirectoryEntry entry= result.GetDirectoryEntry();
try
{
userData.UserID = entry.Properties["employeeID"].Value.ToString();
userData.FirstName =
entry.Properties["givenName"].Value.ToString();
userData.MiddleName =
entry.Properties"middleName"].Value.ToString();
userData.LastName = entry.Properties["sn"].Value.ToString();
userData.FullName =
entry.Properties["displayName"].Value.ToString();
userData.EmailAddress = entry.Properties["mail"].Value.ToString();
userData.Department =
entry.Properties["department"].Value.ToString();
userData.Location =
entry.Properties["physicalDeliveryOfficeName"].Value.ToString();
userData.JobTitle = entry.Properties["title"].Value.ToString();
userData.Description =
entry.Properties["description"].Value.ToString();
userCollection.Add(this.userData);
}
 
W

Willy Denoyette [MVP]

1. You should try not bind to the root of the domain, try binding to the OU
or group instead.
2. You could restrict the properties to load to be the same as the ones you
want to collect.

string[] props = {"employeeID", "givenName", "sn", ....};
directorySearcher.PropertiesToLoad.AddRange(props);
...FindAll();

Willy.


| Hi,
|
| I've written sone C# classes to search the active directory for users -
see
| code below.
|
| The code works fine but is very slow when I'm iterating through the
| directory and getting the attributes via the Properties collection.
|
| Extracting over 500 objects is OK but it takes an extra 15 seconds when
| accesing the data via the Properties["prop name"].value method.
|
| Does anyone know a quicker method - if there is none I'm going to use
| remoting to pre load the information but would be interested to know.
|
| Regards
|
| Mike
|
| public void GetUsers(string domainController)
| {
| DirectorySearcher directorySearcher = new DirectorySearcher(
| new DirectoryEntry("LDAP://" + domainController),
| "(&(objectCategory=Person)(objectClass=user))");
|
| SearchResultCollection entries = directorySearcher.FindAll();
|
| foreach (SearchResult result in entries)
| {
| DirectoryEntry entry= result.GetDirectoryEntry();
| try
| {
| userData.UserID =
entry.Properties["employeeID"].Value.ToString();
| userData.FirstName =
| entry.Properties["givenName"].Value.ToString();
| userData.MiddleName =
| entry.Properties"middleName"].Value.ToString();
| userData.LastName = entry.Properties["sn"].Value.ToString();
| userData.FullName =
| entry.Properties["displayName"].Value.ToString();
| userData.EmailAddress =
entry.Properties["mail"].Value.ToString();
| userData.Department =
| entry.Properties["department"].Value.ToString();
| userData.Location =
| entry.Properties["physicalDeliveryOfficeName"].Value.ToString();
| userData.JobTitle = entry.Properties["title"].Value.ToString();
| userData.Description =
| entry.Properties["description"].Value.ToString();
| userCollection.Add(this.userData);
| }
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top