Click here to Skip to main content
15,314,186 members
Articles / Desktop Programming / MFC
Posted 1 Apr 2003


42 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.73/5 (12 votes)
1 Apr 2003
How to get list of groups user is member of using DirectoryServices in an Active Directory tree.


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();
        DirectoryEntry obEntry = new DirectoryEntry(
        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);
    catch (Exception ex)
    return groups;


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

Web Developer
United States United States
To learn more about us, Please visit us at

Comments and Discussions

Questionhow to get owner of the group Pin
magham19-Aug-10 11:04
Membermagham19-Aug-10 11:04 
AnswerRe: how to get owner of the group Pin
jp2code16-Oct-17 6:51
professionaljp2code16-Oct-17 6:51 
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 22:43
MemberSocrates#11-Apr-10 22:43 
Generalother groups Pin
philthe27-Jan-10 1:38
Memberphilthe27-Jan-10 1:38 
Generalactive directory Pin
sachin arora10-Jun-08 21:23
Membersachin arora10-Jun-08 21:23 
Questionis it recursive? Pin
Refky Wahib21-Apr-07 3:14
MemberRefky Wahib21-Apr-07 3:14 
GeneralWindows NT Pin
Paul Watson2-Apr-03 23:59
sitebuilderPaul Watson2-Apr-03 23:59 
GeneralRe: Windows NT Pin
Rama Krishna Vavilala3-Apr-03 4:37
MemberRama Krishna Vavilala3-Apr-03 4:37 
GeneralRe: Windows NT Pin
Paul Watson3-Apr-03 4:59
sitebuilderPaul Watson3-Apr-03 4:59 
GeneralRe: Windows NT Pin
Aby Louis20-Sep-06 11:16
MemberAby Louis20-Sep-06 11:16 
GeneralRe: Windows NT Pin
Softomatix3-Apr-03 4:52
MemberSoftomatix3-Apr-03 4:52 
GeneralRe: Windows NT Pin
Paul Watson3-Apr-03 5:03
sitebuilderPaul Watson3-Apr-03 5:03 
GeneralRe: Windows NT Pin
Marc Scheuner28-Mar-04 23:58
professionalMarc Scheuner28-Mar-04 23:58 
GeneralRe: Windows NT Pin
anonymous@gmail.com9-Apr-05 0:52
Memberanonymous@gmail.com9-Apr-05 0:52 
GeneralRe: Windows NT Pin
BillJam114-Jun-09 7:43
MemberBillJam114-Jun-09 7:43 

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

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