Click here to Skip to main content
6,596,602 members and growing! (21,941 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate

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

By Softomatix

How to get list of groups user is member of using DirectoryServices in an Active Directory tree.
C++, C#, Windows, .NET 1.0, Visual Studio, MFC, Dev
Posted:1 Apr 2003
Views:83,652
Bookmarked:32 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 4.00 Rating: 4.00 out of 5

1

2
1 vote, 10.0%
3
3 votes, 30.0%
4
6 votes, 60.0%
5

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


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

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
Generalactive directory Pinmembersachin arora22:23 10 Jun '08  
Generalis 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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Apr 2003
Editor: Nishant Sivakumar
Copyright 2003 by Softomatix
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project