|
Question : DirectoryServices Listing User Groups
|
|
i am using the directoryservices class in vb.net and need to be able to return all the users that belong to a particular group.i have code that returns all groups that a user belongs to. here is the code that returns all groups that a user belongs to. in this example i am able to create a directory entry at the top of my domain and perform a search for the samAccountName. i want to be able to do the same but perform a search for the group name and return all members of that group.
Public sub Groups() Dim directory, dir2 As DirectoryEntry Dim searcher As DirectorySearcher Dim sr As SearchResult Dim rpc As ResultPropertyCollection Dim i As Integer
Try directory = New DirectoryEntry("LDAP://" & mvarDomain) searcher = New DirectorySearcher(directory, "(&(objectClass=user)(samAccountName=" & mvarUserName & "))") searcher.PropertiesToLoad.Add("memberof") sr = searcher.FindOne rpc = sr.Properties For i = 0 To (rpc.Item("memberof").Count - 1) dir2 = New DirectoryEntry("LDAP://" & rpc.Item("memberof").Item(i).ToString) MsgBox(dir2.Name) Next Catch ex As Exception Throw (ex) End Try End Function
|
|
Answer : DirectoryServices Listing User Groups
|
|
Take a look at the "member" attribute in AD group schema class. And follow these steps
1. Bind to the given group object 2. Iterate through each member of the "member" attribute, extract and bind using its DN
Dim path As String = "LDAP://..." Dim groupDN = "cn=admin,dn=...." Dim group As DirectoryEntry = new DirectoryEntry(path + groupDN) Dim dn As Object For Each dn in group.Properties["member"] Dim userDN As String = CType( dn, String.GetType() ) Dim user As DirectoryEntry = new DirectoryEntry(path + userDN) Next dn
HTH
|
|
|
|