المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Get all users from a group in ad using directorysearcher



C# Programming
11-19-2009, 01:30 PM
how do i change the filter to only give me users in my group i tryied memberOf='CN=GMDPortal,OU=IT,OU=ADM,DC=corp,DC=com' but it does not work i need to use a directorysearcher because i need to set search.PageSize = 4000; i have 3,000 users in this group

public static ArrayList GetAllADDomainUsers()
{
string domainpath = @"LDAP://DC=corp,DC=com";
ArrayList allusers = new ArrayList();

DirectoryEntry searchRoot = new DirectoryEntry(domainpath);
DirectorySearcher search = new DirectorySearcher(searchRoot);


search.Filter = "(&(objectCategory=person)(objectClass=user)(memberOf='CN=GMDPortal,OU=IT,OU=ADM,DC=corp,DC=com' )(!userAccountControl:1.2.840.113556.1.4.803:=2))";

search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("distinguishedName");
search.PropertiesToLoad.Add("description");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("pwdLastSet");
search.PropertiesToLoad.Add("whenChanged");
search.ReferralChasing = ReferralChasingOption.All;
search.PageSize = 4000;
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
user ousr = new user();
if (resultCol != null)
{
for (
int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains("distinguishedName"))
{
ousr = new user();
ousr.dn = (string)result.Properties["distinguishedName"][0];
try
{
ousr.Description = (string)result.Properties["description"][0];
}
catch (Exception er)
{
ousr.Description = " ";
}
try
{
ousr.DisplayName = (string)result.Properties["displayName"][0];
}
catch (Exception er)
{
ousr.DisplayName = (string)result.Properties["samaccountname"][0];
}
try
{

ousr.pwdLastSet = DateTime.FromFileTime((Int64)result.Properties["pwdLastSet"][0]);
}
catch (Exception er)
{
ousr.pwdLastSet = DateTime.MinValue;
}
try
{
ousr.whenChanged = (DateTime)result.Properties["whenChanged"][0];
}
catch (Exception er)
{
ousr.whenChanged = DateTime.MinValue;
}
ousr.usersname = (string)result.Properties["samaccountname"][0];
allusers.Add(ousr);
}
}
}
return allusers;
}
}

public class user
{
public string dn;
public string usersname;
public string Description;
public string DisplayName;
public DateTime pwdLastSet;
public DateTime whenChanged;
}