get login of members in group

G

Guest

Hello all!
I'm attempting to write a method that will return an arraylist of security
group members. So, what I'd do is pass in a group and return an array. The
problem is, I can't figure out how to return the nt login (CORP\username),
instead I get:
CN=User Name,OU=Users,OU=Support Users,OU=Users and Computers

Here's wht my code is:

private ArrayList _GetADGroupUsers(string groupName)
{
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member"); //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); //original
}
}
return userNames;
}

Help? Thanks!
 
G

Guest

When I tried that though, all I got was the name of the group.
I changed it in all 3 instances. When I just changed it in either
PropertiesToLoad.Add, or in result.Properties["member"], it throws an
exception...

David Jessee said:
get the samAccountName property

Casey said:
Hello all!
I'm attempting to write a method that will return an arraylist of security
group members. So, what I'd do is pass in a group and return an array. The
problem is, I can't figure out how to return the nt login (CORP\username),
instead I get:
CN=User Name,OU=Users,OU=Support Users,OU=Users and Computers

Here's wht my code is:

private ArrayList _GetADGroupUsers(string groupName)
{
SearchResult result;
DirectorySearcher search = new DirectorySearcher();
search.Filter = String.Format("(cn={0})", groupName);
search.PropertiesToLoad.Add("member"); //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); //original
}
}
return userNames;
}

Help? Thanks!
 
G

Guest

*duh* sorry...I didn't read your message really well.
first, when you do your search, you'll want to at least search on
objectClass=group as well as searching againse the cn value.

.....anyway....I digress....

Once you get the group, you'll want to invoke its "Members" method
(remember, this is an object database, where the things in the database have
members) and then enumerate through the resultant user objects.

here's an overview:
http://msdn2.microsoft.com/en-us/library/ms180906.aspx
 

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