Getting users from a specifc AD group

E

egholm

I want to get a list of users from a specific AD group. I use this
code:

****************************************************************************************
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
result = search.FindOne();

ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];
userNames.Add(user);
}
}

****************************************************************************************


When the code is finished user string = "CN=Egholm Jacob (DFDS
A/S),OU=Development,OU=IT,OU=DFDS AS,DC=dk,DC=dfds,DC=root"

But what I want to do is getting information like givenName,
displayName etc. from the user.
What shall I do?
 
W

Willy Denoyette [MVP]

You should load the givenName and displayName property from the AD.

string[] props = {"givenName ", "displayName", ".."};
search.PropertiesToLoad.AddRange(props);
....

Willy.
 
E

egholm

Thanks for your reply. I have change my code to this:

***************************************************************************­*************

SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
search.PropertiesToLoad.Add("displayName");
result = search.FindOne();


ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];

string test =
(string)result.Properties["displayName"][counter];
userNames.Add(user);
}
}
***************************************************************************­*************


The string variable test returns the name of the group and not the name
of the user in the group.
 
W

Willy Denoyette [MVP]

Thanks for your reply. I have change my code to this:

***************************************************************************­*************

SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member");
search.PropertiesToLoad.Add("displayName");
result = search.FindOne();


ArrayList userNames = new ArrayList();
if (result != null)
{
for (int counter = 0; counter <
result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];

string test =
(string)result.Properties["displayName"][counter];
userNames.Add(user);
}
}
***************************************************************************­*************


The string variable test returns the name of the group and not the name
of the user in the group.

If your cn denotes a "username", you should apply the following filter.

src.Filter = "(objectCategory=user)";

also, you should not use a for loop to iterate, use foreach instead.


Willy.
 
E

egholm

Hello Willy

I have change my code (look below), but it still dosen't work.

*****************************************************************************************
DirectoryEntry searchRoot = new DirectoryEntry(@"LDAP://dfds.root");
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(cn=Group CFS Handlers)(objectCategory=user))";
search.PropertiesToLoad.Add("samaccountname");

foreach (SearchResult res in search.FindAll())
{
string dummy = res.Properties["samaccountname"].ToString();
}
*****************************************************************************************

I want to list all the users in the group "Group CFS Handlers", but
search.FindAll() returns 0 results. I just checked the AD and the group
"Group CFS Handlers" contain one user.
I you have any idea what I do wrong, please let my know. I believe it
has something to do with my filter, but I don't know what.
 
E

egholm

Hello Willy

I have change my code (look below), but it still dosen't work.

*****************************************************************************************
DirectoryEntry searchRoot = new DirectoryEntry(@"LDAP://dfds.root");
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(cn=Group CFS Handlers)(objectCategory=user))";
search.PropertiesToLoad.Add("samaccountname");

foreach (SearchResult res in search.FindAll())
{
string dummy = res.Properties["samaccountname"].ToString();
}
*****************************************************************************************

I want to list all the users in the group "Group CFS Handlers", but
search.FindAll() returns 0 results. I just checked the AD and the group
"Group CFS Handlers" contain one user.
I you have any idea what I do wrong, please let my know. I believe it
has something to do with my filter, but I don't know what.
 

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