Trying to get Users in a Group

E

Ed_P.

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=net");

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?
 
M

Mark Rae

Am I missing something, or is there a better way to search for and
retrieve users from a group?

Not necessarily better...

using System;
using System.Collections;
using System.DirectoryServices;

public static Hashtable GetUsersInGroup(string pstrDomain, string pstrGroup)
{
DirectoryEntry objADEntry = null;
DirectoryEntry objGroup = null;
object objMembers = null;
Hashtable htblUsers = new Hashtable();

try
{
objADEntry = new DirectoryEntry("WinNT://" + pstrDomain + ",
domain");
objGroup = objADEntry.Children.Find(pstrGroup, "group");
objMembers = objGroup.Invoke("Members");
foreach (object objMember in (IEnumerable)objMembers)
{
DirectoryEntry objUser = new DirectoryEntry(objMember);
htblUsers.Add(objUser.Name, GetObjectProperty(objUser.Name,
"FullName"));
}
return htblUsers;
}
catch (Exception)
{
throw;
}
}
 
W

Willy Denoyette [MVP]

Ed_P. said:
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=net");

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top