Click here to Skip to main content
Click here to Skip to main content

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

, 1 Apr 2003
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Softomatix
Web Developer
United States United States
To learn more about us, Please visit us at http://www.netomatix.com

Comments and Discussions

 
Questionhow to get owner of the group Pinmembermagham19-Aug-10 11:04 
GeneralThis is fine example of how NOT to enumerate user groups, and how to unleash hell on your domain controllers PinmemberSocrates#11-Apr-10 22:43 
Generalother groups Pinmemberphilthe27-Jan-10 1:38 
Generalactive directory Pinmembersachin arora10-Jun-08 21:23 
Questionis it recursive? PinmemberRefky Wahib21-Apr-07 3:14 
GeneralWindows NT PinsitebuilderPaul Watson2-Apr-03 23:59 
GeneralRe: Windows NT PinmemberRama Krishna3-Apr-03 4:37 
Do you want to see if a user is in a praticular group or do you want to list all the gropus user belongs to?
 
If you want to see if the user belongs to a praticular group you can easily use "User.IsInRole" method.
 

GeneralRe: Windows NT PinsitebuilderPaul Watson3-Apr-03 4:59 
GeneralRe: Windows NT PinmemberAby Louis20-Sep-06 11:16 
GeneralRe: Windows NT PinmemberSoftomatix3-Apr-03 4:52 
GeneralRe: Windows NT PinsitebuilderPaul Watson3-Apr-03 5:03 
GeneralRe: Windows NT PinmemberMarc Scheuner28-Mar-04 23:58 
GeneralRe: Windows NT PinsussAnonymous9-Apr-05 0:52 
GeneralRe: Windows NT PinmemberBillJam114-Jun-09 7:43 

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.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 2 Apr 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid