"Ed_P." <ed**@no-email.com> wrote in message
news:Ox**************@TK2MSFTNGP12.phx.gbl...
Hello,
I am trying to retrieve the list of users that belong to a security group
Active Directory. But I am unable to retrieve the users. Below you will
find the code that I am using, please note that the LDAP string is
pointing to a Security Group that has about 6 users in it.
DirectoryEntry entry = new
DirectoryEntry("LDAP://win2k3domain.net/CN=ExternalUsers,OU=KB_Users,DC=win2k3domain,DC=ne t");
foreach(DirectoryEntry child in entry.Children)
{
Console.WriteLine("Name: {0}", child.Name); //not retrieving anything
}
Am I missing something, or is there a better way to search for and
retrieve users from a group?
Children returns DirectoryEntries, these can contain other groups as well,
so you'll have to filter the resultset. Also you might not need all
properties associated with an entry, so you'll better use a DirectorySeacher
so you can specify the properties you need.
Following is a small sample that illustrates how you can specify the
properties you need, the number of entries per page for a pages search and
the scope level of the search...
using(DirectoryEntry entry = new DirectoryEntry(LDAP://...........)
{
using(DirectorySearcher src = new DirectorySearcher())
{
string[] props = {"sn", "givenname", "sAMAccountName",
"userAccountControl"};
src.PropertiesToLoad.AddRange(props);
src.SearchRoot = ent;
src.SearchScope = SearchScope.OneLevel;
src.Filter = "(objectCategory=user)";
src.PageSize = 500; // In pages of 500 entries
SearchResultCollection res = src.FindAll();
foreach(SearchResult sc in res)
{
foreach(string myKey in res.PropertiesLoaded)
{
Console.Write(myKey + " = ");
foreach( Object myCollection in sc.Properties[myKey])
{
Console.WriteLine(myCollection);
}
}
}
}
}
Willy.