5,691,626 members and growing! (13,189 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, .NET, Visual Studio, MFC, Dev

Posted: 1 Apr 2003
Updated: 1 Apr 2003
Views: 74,112
Bookmarked: 28 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 4.00 Rating: 4.00 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
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


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
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
Generalactive directorymembersachin arora22:23 10 Jun '08  
Generalis it recursive?memberRefky Wahib4:14 21 Apr '07  
GeneralWindows NTsitebuilderPaul Watson0:59 3 Apr '03  
GeneralRe: Windows NTmemberRama Krishna5:37 3 Apr '03  
GeneralRe: Windows NTsitebuilderPaul Watson5:59 3 Apr '03  
GeneralRe: Windows NTmemberAby Louis12:16 20 Sep '06  
GeneralRe: Windows NTmemberSoftomatix5:52 3 Apr '03  
GeneralRe: Windows NTsitebuilderPaul Watson6:03 3 Apr '03  
GeneralRe: Windows NTmemberMarc Scheuner0:58 29 Mar '04  
GeneralRe: Windows NTsussAnonymous1:52 9 Apr '05  

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-2008
Web13 | Advertise on the Code Project