Home All Groups Group Topic Archive Search About

get login of members in group

Author
14 Aug 2006 6:00 PM
Casey
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!

Author
14 Aug 2006 7:18 PM
David Jessee
get the samAccountName property

Show quote
"Casey" wrote:

> 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!
Author
14 Aug 2006 8:26 PM
Casey
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...

Show quote
"David Jessee" wrote:

> get the samAccountName property
>
> "Casey" wrote:
>
> > 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!
Author
14 Aug 2006 9:22 PM
David Jessee
*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




Show quote
"Casey" wrote:

> 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!

AddThis Social Bookmark Button