Click here to Skip to main content
11,483,586 members (71,276 online)
Click here to Skip to main content

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

, 1 Apr 2003 149.2K 40
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 Pin
magham19-Aug-10 12:04
membermagham19-Aug-10 12:04 
GeneralThis is fine example of how NOT to enumerate user groups, and how to unleash hell on your domain controllers Pin
Socrates#11-Apr-10 23:43
memberSocrates#11-Apr-10 23:43 
Generalother groups Pin
philthe27-Jan-10 2:38
memberphilthe27-Jan-10 2:38 
Generalactive directory Pin
sachin arora10-Jun-08 22:23
membersachin arora10-Jun-08 22:23 
Questionis it recursive? Pin
Refky Wahib21-Apr-07 4:14
memberRefky Wahib21-Apr-07 4:14 
GeneralWindows NT Pin
Paul Watson3-Apr-03 0:59
sitebuilderPaul Watson3-Apr-03 0:59 
GeneralRe: Windows NT Pin
Rama Krishna3-Apr-03 5:37
memberRama Krishna3-Apr-03 5:37 
GeneralRe: Windows NT Pin
Paul Watson3-Apr-03 5:59
sitebuilderPaul Watson3-Apr-03 5:59 
GeneralRe: Windows NT Pin
Aby Louis20-Sep-06 12:16
memberAby Louis20-Sep-06 12:16 
GeneralRe: Windows NT Pin
Softomatix3-Apr-03 5:52
memberSoftomatix3-Apr-03 5:52 
GeneralRe: Windows NT Pin
Paul Watson3-Apr-03 6:03
sitebuilderPaul Watson3-Apr-03 6:03 
GeneralRe: Windows NT Pin
Marc Scheuner29-Mar-04 0:58
memberMarc Scheuner29-Mar-04 0:58 
GeneralRe: Windows NT Pin
Anonymous9-Apr-05 1:52
sussAnonymous9-Apr-05 1:52 
GeneralRe: Windows NT Pin
BillJam114-Jun-09 8:43
memberBillJam114-Jun-09 8: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 | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 2 Apr 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid