Error in System.DirectoryServices.AccountManagement?

The following C # code (.NET Framework 3.5) returns the name and description of all users in the AD group "xyz". It works fine while it returns multiple records. But it is very slow when it returns more than 100 records. Any suggestions would be greatly appreciated. Thank you in advance!

var context = new PrincipalContext(ContextType.Domain);

var grp = GroupPrincipal.FindByIdentity(context, "xyz");

var users = grp.GetMembers(true);

var usersList = users.Select(n => new { UserName = n.Name, 
                                        Description = n.Description })
                      .OrderBy(o => o.UserName.ToString());

Console.WriteLine(usersList.ToList());
+3
source share
1 answer

You will have better performance querying the attribute area (ASQ). Here is a sample code:

DirectoryEntry group = new DirectoryEntry("LDAP://CN=All Staff,OU=Groups,DC=domain,DC=local");

DirectorySearcher searcher = new DirectorySearcher();
searcher.SearchRoot = group;
searcher.Filter =
   "(&(objectClass=user)(objectCategory=person)(mail=*))";
searcher.PropertiesToLoad.Add("mail");
searcher.SearchScope = SearchScope.Base;

searcher.AttributeScopeQuery = "member";

List<string> mail;
using (SearchResultCollection results = searcher.FindAll())
{
   mail = new List<string>();
   foreach (SearchResult result in results)
   {
        mail.Add(result.Properties["mail"][0].ToString());
   }
}

, System.DirectoryServices.AccountManagement , System.DirectoryServices. 99% . , LDAP .

, 1000 . , , , , .

+4

Source: https://habr.com/ru/post/1732798/


All Articles