Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to see all the active directory groups that a user is a member of? I am using the below code but it lists all the groups in the AD. I need to see only the groups for username "domain\cc6900"

VB
 Public Function GetGroups() As List(Of String)
        Dim objADAM As DirectoryEntry                  
        Dim objGroupEntry As DirectoryEntry            
        Dim objSearchADAM As DirectorySearcher          
        Dim objSearchResults As SearchResultCollection  
        Dim strPath As String                           
        Dim result As New List(Of String)

        
        strPath = "LDAP://Domain.local"
               Try
            objADAM = New DirectoryEntry(strPath)
            objADAM.RefreshCache()
        Catch e As Exception
            Throw e
        End Try

              Try
            objSearchADAM = New DirectorySearcher(objADAM)
            objSearchADAM.Filter = "(&(objectClass=group))"
            objSearchADAM.SearchScope = SearchScope.Subtree
            objSearchResults = objSearchADAM.FindAll()
        Catch e As Exception
            Throw e
        End Try

        
        Try
            If objSearchResults.Count <> 0 Then
                Dim objResult As SearchResult
                For Each objResult In objSearchResults
                    objGroupEntry = objResult.GetDirectoryEntry
                    result.Add(objGroupEntry.Name)

                Next objResult
            Else
                Throw New Exception("No groups found")
            End If
        Catch e As Exception
            Throw New Exception(e.Message)
        End Try

        Return result
    End Function

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
       For Each group As String In GetGroups()
           
            lstGroups.Items.Add(group)

       Next

   End Sub
Posted
Comments
Prasad Khandekar 25-Mar-13 6:37am    
Hello,

The memberOf attribute on user will give you a list of groups user belongs to.

1 solution

You are not passing your username anywhere in your code. Also, in Active Directory the groups that a user belongs is "member of". The link below should help. You may need to tweak it to get ALL groups(get the groups that each group is a member as well).


http://kellyschronicles.wordpress.com/2008/07/25/getting-all-groups-that-an-active-directory-user-is-a-member-of-in-vb-net/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900