I'm not sure if there is another method, but this is the method I used for checking if a user belongs to a particular group. It can be altered to return the group:
Added to the code window.
Code Snippet
Function IsMemberOfGroup(ByVal UserName As String, ByVal GroupName As String) As Boolean
Try
Dim ent As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("LDAP://DC=domainname,DC=com")
Dim srch As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher("(CN=" + GroupName + ")")
Dim coll As System.DirectoryServices.SearchResultCollection = srch.FindAll()
For Each rs As System.DirectoryServices.SearchResult In coll
Dim resultPropColl As System.DirectoryServices.ResultPropertyCollection = rs.Properties
For Each memberColl As Object In resultPropColl("member")
Dim gpMemberEntry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" + memberColl.ToString)
Dim userProps As System.DirectoryServices.PropertyCollection = gpMemberEntry.Properties
Dim obVal As Object = userProps("sAMAccountName").Value
If obVal.ToString = UserName Then Return True
Next
Next
Catch ex As Exception
'Trace.Write(ex.Message)
End Try
Return False
End Function
No comments:
Post a Comment