4

I have a list with all of the AD usernames (over 1,500), and I would like to know what would be the best way to retrieve the email address for all users contained within that list.

Specifically, a method that I can call and supply it with a List of usernames and have the method return a list of the same size that contains the email addresses for all of the usernames supplied on the initial list.

I already know how to retrieve the email address using the conventional LDAP way as shown in http://lozanotek.com/blog/articles/149.aspx , and I know I could use a Foreach, however I am looking for a more efficient way to retrieve over 1,500 email addresses without querying AD over 1,500 times.

4

3 回答 3

2

嘿,我一直在寻找相同的东西,这对我有用:http ://forums.asp.net/t/1610245.aspx/1

于 2013-01-02T17:38:54.947 回答
0

Using the method in your link you would not be querying AD 1500 times, you would be running one query that returns all 1500 users as objects. Then using a foreach loop you would iterate through each object to get the user's emails.

An example that would query AD 1500 times would be if you had an ArrayList of all user names and did the following (pseudo-code):

ArrayList UserNames = new ArrayList();
Foreach(string name in UserNames)
{
   //Query AD to get email of user by passing in name
}

One way to make this faster would be to add multi-threading and have each thread handle a different query to AD at the same time. For instance, you could add filters to your query so that it only returns names that start with A and have one thread handle that, while another handles names that start with B and so on. This would require more queries to AD (26, 1 for each letter in the alphabet) but you could handle the returned data in parallel.

Unless you have really old domain controllers I wouldn't worry about hitting AD hard, it is made for this. I cache all of AD (users, groups, computers and relationships between them) into a database nightly and we have over 8000 users. I do this because calls to AD tend to be slow and we wanted group membership information to be available through a webpage without users waiting forever. This caching makes thousands of calls to AD a minute and this does not disturb the workflow of my company during its 3 hour run time.

Update:
Sorry I read over your question quickly and missed that you are attempting the method with a list of all names. You can either use the multi-threaded method as I stated before but pass each user name to be handled to a different thread (limiting the number of threads so it does not go out of control!!) or use the one query to get all users and check to see if they exist in your list within the foreach loop. By making your list easy to traverse using a binary tree or another sorted collection, seeing if a user exists should be fairly quick. You can also remove users from your list as they are found to make subsequent searches faster.

于 2013-01-02T17:49:34.807 回答
0

我在下面使用了这段代码,但现在我意识到这与 Srinivas 所说的代码相同。

DirectoryEntry entry = new DirectoryEntry("LDAP://YourDomain"); 
DirectorySearcher dSearch = new DirectorySearcher(entry); 
dSearch.Filter = "(objectClass=user)"; 
foreach (SearchResult sResultSet in dSearch.FindAll()) 
{ 
    if (sResultSet.Properties["mail"].Count > 0) 
        Response.Write(sResultSet.Properties["mail"][0].ToString() + "<br/>"); 
}
于 2013-01-02T18:01:10.603 回答