Click here to Skip to main content
Licence 
First Posted 1 Apr 2003
Views 109,454
Bookmarked 36 times

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

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

1

2
1 vote, 10.0%
3
3 votes, 30.0%
4
6 votes, 60.0%
5
4.00/5 - 10 votes
μ 4.00, σa 1.24 [?]

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 Pinmembermagham12: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#23:43 11 Apr '10  
Generalother groups Pinmemberphilthe2:38 27 Jan '10  
Generalactive directory Pinmembersachin arora22:23 10 Jun '08  
Questionis it recursive? PinmemberRefky Wahib4:14 21 Apr '07  
GeneralWindows NT PinsitebuilderPaul Watson0:59 3 Apr '03  
GeneralRe: Windows NT PinmemberRama Krishna5:37 3 Apr '03  
GeneralRe: Windows NT PinsitebuilderPaul Watson5:59 3 Apr '03  
GeneralRe: Windows NT PinmemberAby Louis12:16 20 Sep '06  
GeneralRe: Windows NT PinmemberSoftomatix5:52 3 Apr '03  
GeneralRe: Windows NT PinsitebuilderPaul Watson6:03 3 Apr '03  
GeneralRe: Windows NT PinmemberMarc Scheuner0:58 29 Mar '04  
GeneralRe: Windows NT PinsussAnonymous1:52 9 Apr '05  
GeneralRe: Windows NT PinmemberBillJam118: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
Web01 | 2.5.120210.1 | Last Updated 2 Apr 2003
Article Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid