I use System.DirectoryServices.AccountManagement
to query Active Directory for a single user info
public UserInfo FindOne(string samUserName)
{
using (var ctx = new PrincipalContext(ContextType.Domain, "domain.com", "Bob", "pwd"))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, samUserName))
{
if (user != null)
{
// get info about Alice into userInfo
return userInfo;
}
}
}
return null;
}
So if I use var aliceInfo = search.FindOne("alice");
I get info from the directory. Now I need to search a directory (1000+ users) given several user logon names, for example
var userInfos = search.FindMany(/* list of names: alice, jay, harry*/);
How to implement the following method?
public List<UserInfo> FindMany(List<string> samUserNames)
{
...
}
If your list is relatively small, the most flexible solution will probably be to loop and look up the users one by one.
The alternatives are:
Provide a filter in the LDAP query. Since you have no common attribute to filter on, you would need to create an "OR" LDAP filter with all of the usernames. Which doesn't really scale to a large number of users any better than looping.
Iterate over all users in the directory, filtering the search results to extract the ones that match your list. This doesn't scale well to a large AD, where it doesn't take advantage of the fact that samAccountName is an indexed property.