1

I use following code to retrieve all users of active directory but not all users retrieved. Should I include something else in Filter? this is simplified code.

DirectoryEntry entry = new DirectoryEntry("LDAP://" + Bous.DomainName, Bous.UserName, Bous.Password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("(&(objectCategory=person)(objectClass=user))");

foreach (SearchResult result in mySearcher.FindAll())
{
     dr = dt.NewRow();
     ResultPropertyCollection myResultPropColl = result.Properties;
     dr[0] = myResultPropColl["samaccountname"][0].ToString() + "@" + Bous.DomainName;
     dt.Rows.Add(dr);
}        
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Raymond Morphy
  • 2,464
  • 9
  • 51
  • 93
  • 1
    AFAIK there is a limit regarding number of entities can be retrieved from AD. check http://stackoverflow.com/questions/3488394/c-sharp-active-directory-services-findall-returns-only-1000-entries – daryal Dec 24 '13 at 09:18

3 Answers3

1

Try this one:

DirectoryEntry entry = new DirectoryEntry("LDAP://" + Bous.DomainName, Bous.UserName, Bous.Password);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.PageSize = 1000;   // <--- Important!
mySearcher.Filter = ("(&(objectCategory=person)(objectClass=user))");

foreach (SearchResult result in mySearcher.FindAll())
{
     dr = dt.NewRow();
     ResultPropertyCollection myResultPropColl = result.Properties;
     dr[0] = myResultPropColl["samaccountname"][0].ToString() + "@" + Bous.DomainName;
     dt.Rows.Add(dr);
}     
Scoregraphic
  • 7,110
  • 4
  • 42
  • 64
1

If you're on .NET 3.5 or newer, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // define a "query-by-example" principal - here, we search for UserPrincipal (users)
   UserPrincipal qbeUser = new UserPrincipal(ctx);

   // create your principal searcher passing in the QBE principal    
   PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

   // find all matches
   foreach(var found in srch.FindAll())
   {
       // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
   }
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement. Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    ...
    protected static UserPrincipal QueryFilterUsers(string FilterFullName, string FilterDescription, string FilterLogin, string FilterPhone, string FilterEmail)
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, _Domain);
        UserPrincipal _UserPrincipal = new UserPrincipal(ctx);

        _UserPrincipal.SamAccountName = "*" + ((!string.IsNullOrEmpty(FilterLogin)) ? FilterLogin.Trim() + "*" : "");
        if (!string.IsNullOrEmpty(FilterFullName)) _UserPrincipal.DisplayName = "*" + FilterFullName.Trim() + "*";
        if (!string.IsNullOrEmpty(FilterDescription)) _UserPrincipal.Description = "*" + FilterDescription.Trim() + "*";
        if (!string.IsNullOrEmpty(FilterPhone)) _UserPrincipal.VoiceTelephoneNumber = "*" + FilterPhone.Trim() + "*" ;
        if (!string.IsNullOrEmpty(FilterEmail)) _UserPrincipal.EmailAddress = "*" + FilterEmail.Trim() + "*";
        return _UserPrincipal;
    }

    //Получение списков
    public static List<Principal> GetUsersPrincipalList(string FilterFullName, string FilterDescription, string FilterLogin, string FilterPhone, string FilterEmail)
    {
        PrincipalSearcher _PrincipalSearcher = new PrincipalSearcher();
        _PrincipalSearcher.QueryFilter = QueryFilterUsers(FilterFullName, FilterDescription, FilterLogin, FilterPhone, FilterEmail);
        (_PrincipalSearcher.GetUnderlyingSearcher() as DirectorySearcher).PageSize = _PageSize;
        (_PrincipalSearcher.GetUnderlyingSearcher() as DirectorySearcher).SizeLimit = _SizeLimit;

        return _PrincipalSearcher.FindAll().ToList();
    }
dima_horror
  • 3,098
  • 1
  • 20
  • 24