I'm trying to get some information from Active Directory with VB.NET. I have the "primaryGroupID" of a user, which is 2096 in this case. How can I get, with VB.NET, the CN of this group?
Ultimately, what I need to do is find a list of groups that a user belongs to (including groups that belong to another group). I already have a function that gets the main groups except for the primary group, and another function that returns the ID of the primary group. Both detailed below.
Public Function getUserGroups(ByVal Username)
Dim grupos As New ArrayList()
Try
Dim Entry As New System.DirectoryServices.DirectoryEntry(ldapPath, ldapAdminUser, ldapAdminPass)
Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
Searcher.SearchScope = DirectoryServices.SearchScope.Subtree
Searcher.Filter = "(&(objectcategory=user)(SAMAccountName=" & Username & "))"
Dim res As SearchResult = Searcher.FindOne
For i = 0 To res.Properties("memberOf").Count() - 1
grupos.Add(res.Properties("memberOf")(i).ToString)
Next
Catch ex As Exception
End Try
Return grupos
End Function
Public Function GetUserPrimaryGroupID(ByVal user As String) As String
Dim grupoID As String = ""
Try
Dim Entry As New System.DirectoryServices.DirectoryEntry(ldapPath, ldapAdminUser, ldapAdminPass)
Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
Searcher.SearchScope = DirectoryServices.SearchScope.Subtree
Searcher.Filter = "(&(objectcategory=user)(SAMAccountName=" & user & "))"
Dim res As SearchResult = Searcher.FindOne
For i = 0 To res.Properties("primaryGroupID").Count() - 1
grupoID = (res.Properties("primaryGroupID")(i).ToString) 'Esto devuelve la ruta "CN" del grupo
'grupoID = (res.Properties("primaryGroupID")(i).ToString)
'Dim de As DirectoryEntry = New DirectoryEntry("LDAP://" + res.Properties("primaryGroupID")(i).ToString())
Next
Catch ex As Exception
End Try
Return grupoID
End Function