Click here to Skip to main content
Licence 
First Posted 1 Apr 2003
Views 113,121
Bookmarked 37 times

How to get the list of groups that a user is a member of

By | 1 Apr 2003 | Article
How to get list of groups user is member of using DirectoryServices in an Active Directory tree.

Introduction

In the previous article, How to get members of a group using DirectoryServices we showed how you can get list of members in a group. In this article we will show you the other way i.e. how to get list of groups that a user belongs to. There is no direct call in DirectoryServices namepsace that will get this accomplished. You can use DirectorySearcher class to get the user object. And then call Invoke method to call Groups method defined in ADSI.

Code Listing

private void Page_Load(object sender, System.EventArgs e)
{
    StringCollection groups = this.GetUserGroupMembership("foo");
    foreach (string gp in groups)
    {
        Response.Write("<br><b>" + gp + "</b>");
    }
}

private StringCollection GetUserGroupMembership(string strUser)
{
    StringCollection groups = new StringCollection();
    try
    {
        DirectoryEntry obEntry = new DirectoryEntry(
            "LDAP://CN=users,DC=pardesifashions,DC=com");
        DirectorySearcher srch = new DirectorySearcher(obEntry, 
            "(sAMAccountName=" + strUser + ")");
        SearchResult res = srch.FindOne();
        if (null != res)
        {
            DirectoryEntry obUser = new DirectoryEntry(res.Path);
            // Invoke Groups method.
            object obGroups = obUser.Invoke("Groups");
            foreach (object ob in (IEnumerable)obGroups)
            {
                // Create object for each group.
                DirectoryEntry obGpEntry = new DirectoryEntry(ob);
                groups.Add(obGpEntry.Name);
            }
        }
    }
    catch (Exception ex)
    {
        Trace.Write(ex.Message);
    }
    return groups;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Softomatix

Web Developer

United States United States

Member

To learn more about us, Please visit us at http://www.netomatix.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhow to get owner of the group Pinmembermagham11:04 19 Aug '10  
GeneralThis is fine example of how NOT to enumerate user groups, and how to unleash hell on your domain controllers PinmemberSocrates#22:43 11 Apr '10  
Generalother groups Pinmemberphilthe1:38 27 Jan '10  
Generalactive directory Pinmembersachin arora21:23 10 Jun '08  
Questionis it recursive? PinmemberRefky Wahib3:14 21 Apr '07  
GeneralWindows NT PinsitebuilderPaul Watson23:59 2 Apr '03  
GeneralRe: Windows NT PinmemberRama Krishna4:37 3 Apr '03  
GeneralRe: Windows NT PinsitebuilderPaul Watson4:59 3 Apr '03  
GeneralRe: Windows NT PinmemberAby Louis11:16 20 Sep '06  
GeneralRe: Windows NT PinmemberSoftomatix4:52 3 Apr '03  
GeneralRe: Windows NT PinsitebuilderPaul Watson5:03 3 Apr '03  
GeneralRe: Windows NT PinmemberMarc Scheuner23:58 28 Mar '04  
GeneralRe: Windows NT PinsussAnonymous0:52 9 Apr '05  
I agree, Marc. And DirectorySearcher.FindOne method in the code could also cause memory leak problem.
GeneralRe: Windows NT PinmemberBillJam117:43 4 Jun '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120528.1 | Last Updated 2 Apr 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid