• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

Getting Users Group Membership In Active Directory

Hi all

I am trying to get what groups a user is in.  I can easily get my own groups, but I can't remember how to enter a name into a text box, hit search and let my app go pull back the details.  

I know there must be a way that I can enter myoung and get all groups for that user.

All help is apperciated.
0
angus_young_acdc
Asked:
angus_young_acdc
  • 11
  • 5
  • 4
  • +1
8 Solutions
 
DhaestCommented:
Howto: (Almost) Everything In Active Directory via C#

http://www.codeproject.com/KB/system/everythingInAD.aspx#39
0
 
angus_young_acdcAuthor Commented:
Hi ged, that link looks like it does what I already have (only in VB).  I don't want to get GetCurrent() as I want to check other usernames bar my own.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DhaestCommented:
0
 
angus_young_acdcAuthor Commented:
Hi Dhaest, that VB code just seems to be counting something.  What I am trying to achieve is that I type a username into a textbox (eg dhaest if that was your computer logon) and then a list box is populated with all the groups you are a member of.  

Doesn't seem like I can do this without the user:
a) giving me their password
b) using it themselves

Neither of those options - especially a - I am keen on.  I guess I can't do it :(
0
 
DhaestCommented:
Perhaps you should take a look at LINQ to active directory
http://www.codeplex.com/LINQtoAD

I even think that this post is the solution for your problem:
http://www.henrycordes.nl/post/2008/01/LINQ-to-Active-Directory.aspx
0
 
angus_young_acdcAuthor Commented:
I can't seem to download the source material for that last link (apparently I need the following assembly: BdsSoft.DirectoryServices.Linq)
0
 
DhaestCommented:
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:

Hi ged, that link looks like it does what I already have (only in VB).  I don't want to get GetCurrent() as I want to check other usernames bar my own.

 - Replace the get current with the call to a username that you want to look up.

Pass it as a string param.
0
 
AmericomCommented:
Try this, it will detect your default domain and logged on user account. But you can input your domain or user account as you wish.

GetUserMemberOf.txt
0
 
AmericomCommented:
By the way, just rename to extension to VBS from TXT
0
 
angus_young_acdcAuthor Commented:
Hi Americom
I don't suppose you know what that code would be in C#?  Will try and see if I can use it.

ged325
I don't see a method to do that.  Can you tell me where it is?

Dhaest
I'm looking through that link now, as per your suggestion I managed to get the source code downloaded at last :)
0
 
angus_young_acdcAuthor Commented:
Have also tried the code in the snippet below, but I get "The server is not operational." as an exception returned.
            using(DirectoryEntry root = new DirectoryEntry("LDAP://"+domain))
            {
                using (DirectorySearcher ds = new DirectorySearcher(root))
                {
                    SearchResult sr = null;
                    ds.Filter = "(SAMAccountName=" + txtUser.Text + ")";
                    sr = ds.FindOne();
                    using (DirectoryEntry user = sr.GetDirectoryEntry())
                    {
                        System.DirectoryServices.PropertyCollection pcoll = user.Properties;
                        PropertyValueCollection memberOf = pcoll["memberOf"];
                        foreach (string cnGroup in memberOf)
                        {
                            ds.Filter = cnGroup.Substring(0, cnGroup.IndexOf(','));
                            sr = ds.FindOne();
                            using (DirectoryEntry group = sr.GetDirectoryEntry())
                            {
                                lstGroups.Items.Add(group.Properties["SAMAccountName"].Value.ToString());
                            }
                        }
                    }
                }
            }

Open in new window

0
 
angus_young_acdcAuthor Commented:
Update:  The following code DOES return what I want deep, deep down.  But I can't seem to actually extract the group list.  If I go a quickwatch it is far down in the properties, but I can't seem to access it.
DirectorySearcher search = new DirectorySearcher("LDAP://"+domain);
            search.Filter = "(SAMAccountName=" + userName + ")";
           // search.PropertiesToLoad.Add("memberOf");
            search.PropertiesToLoad.Add("memberOf");
 
            SearchResultCollection result = search.FindAll();
            foreach (SearchResult res in result)
            {
                lstGroups.Items.Add(res.Properties.Count.ToString());
            }

Open in new window

0
 
angus_young_acdcAuthor Commented:
Anybody able to help out?  
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
looks like your missing a few lines?
       ADSearch.SearchScope = SearchScope.Subtree
        Dim UserFound As SearchResult = ADSearch.FindOne()
        Dim PropertyCount As Integer = UserFound.Properties("memberOf").Count
0
 
angus_young_acdcAuthor Commented:
Hi ged325

I put in that the scope is the subtree like your suggestion, but I'm still unable to push the data (the groups) into the list box.  I can still see it all when I do a quick watch, but I just can't get it out of the SearchResult
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post a screen of your quick watch?  What's happening when you step through?
0
 
angus_young_acdcAuthor Commented:
Unfortunately I can't post a picture as it is blocked here.   When my code reaches this line:
foreach(SearchResult res in result)

If I do a quick watch and go through the tree as follows:
res -> Properties -> Values -> Non-public members -> _hashtable -> ["memberof"] -> Value - > InnerList

Then I can see all the groups I am after.  How can I get this list out?
0
 
angus_young_acdcAuthor Commented:
Just sorted it myself with the following code:

            foreach (SearchResult res in result)
            {
                foreach (ResultPropertyValueCollection arr in res.Properties.Values)
                {
                    foreach (object obj in arr)
                    {
                        lstGroups.Items.Add(obj);
                    }
                }
            }
            return idList;

Open in new window

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
glad you were able to work it out.  
0
 
angus_young_acdcAuthor Commented:
Finished the solution myself, however I am very greatful for the help provided by each member.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 11
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now