Solved

C# Get Active Directory Members from a Distribution Group

Posted on 2009-04-07
3
1,786 Views
Last Modified: 2012-05-06
Hi,

I am trying to retrieve all the members from Active Directory using C# AD services.

I am able to retrieve all the members from the top level group. In our organisation they have defined some of Group Names start with * symbol. I am not able to locate the AD Group directly using the distinguishedName property.

This problem I am facing only if such group is added into some other group. for ex.

Group1 //(Main Group) I am able to locate the group
     Member1 //(Members) I am able to retrieve the members
     Member2
     Member3
    Group11 //(Sub Group) I am able to locate the group.
    *Group12 //(Sub Group with * symbol) I am unable to locate the group.

I need some help on this.
DirectoryEntry gc;

DirectoryEntry searchRoot = null;

DirectorySearcher searcher;

SearchResultCollection result;
 

// Get the directoryentry of the Global Catalog root

gc = new DirectoryEntry("GC:");
 

foreach (DirectoryEntry child in gc.Children)

{

          searchRoot = child;

}
 

searcher = new DirectorySearcher(searchRoot, string.Format("(&(|(objectCategory=person)(objectCategory=group))(sAMAccountName={0}))", searchName), new string[] { "distinguishedName", "mail", "sAMAccountName" }, SearchScope.Subtree);

                

SearchResult searchResult = searcher.FindOne();
 

if (searchResult != null)

{

        DirectoryEntry entry = new DirectoryEntry(searchResult.Path);
 

         if (IsGroup(GetValue(entry, "objectCategory")))

         {

                 IterateGroup(entry, searchRoot);

         }

         else

         {

               textBox2.AppendText(GetValue(entry, "GivenName") + " " + GetValue(entry, "SN"));

               textBox2.AppendText(Environment.NewLine);

         }

}

else

         textBox2.AppendText("User/Group not found in Active Directory");
 
 

private void IterateGroup(DirectoryEntry groupentry, DirectoryEntry searchRoot)

        {

            foreach (object member in groupentry.Properties["member"])

            {

                DirectorySearcher searcher = new DirectorySearcher(searchRoot, string.Format("(&(|(objectCategory=person)(objectCategory=group))(distinguishedName={0}))", member), new string[] { "distinguishedName", "mail", "sAMAccountName" }, SearchScope.Subtree);
 

                SearchResult searchResult = searcher.FindOne();
 

                if (searchResult != null)

                {

                    DirectoryEntry entry = searchResult.GetDirectoryEntry();
 

                    if (IsGroup(GetValue(entry, "objectCategory")))

                    {

                        IterateGroup(entry, searchRoot);

                    }

                    else

                    {

                        textBox2.AppendText(GetValue(entry, "GivenName") + " " + GetValue(entry, "SN"));

                        textBox2.AppendText(Environment.NewLine);

                    }

                }

            }

        }

Open in new window

0
Comment
Question by:GiftsonDJohn
  • 2
3 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24084650
This is perhaps not the answer you want, but it can be a usefull resource for you.
Howto: (Almost) Everything In Active Directory via C#
http://www.codeproject.com/KB/system/everythingInAD.aspx
0
 
LVL 14

Author Comment

by:GiftsonDJohn
ID: 24085450
Hi Dhaest,

Thanks for your reply. I know about it. I am able to retrieve all the members of the group. I am not able to get only when the name of the group starts like *GroupName. I need assistance to overcome this.
0
 
LVL 14

Accepted Solution

by:
GiftsonDJohn earned 0 total points
ID: 24838138
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now