Here is an example of obtaining Group Membership for a user.
This code was obtained via web search when I needed the functionality for an application. Please excuse the lack of comments on how the code works. Maybe other forum members with more experience in this area can elaborate on its flow process.
Example: dim ret As Boolean = IsMemberOfGroup("James","Accounting")
Public Function IsMemberOfGroup(ByVal strUserID As String, ByVal strGroupName As String) As Boolean
Dim blnIsMemberOfGroup As Boolean = False
Try
Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)
Dim ADSearch As New System.DirectoryServices.DirectorySearcher(ADEntry )
If strUserID = "" Then
strUserID = System.Security.Principal.WindowsIdentity.GetCurre nt.Name.Split("\"c)(1)
End If
Dim ADSearchResult As System.DirectoryServices.SearchResult = Nothing
ADSearch.PropertiesToLoad.Add("memberOf")
ADSearch.Filter = ("(samAccountName=" & strUserID & ")")
ADSearch.SearchScope = System.DirectoryServices.SearchScope.Subtree
Dim UserFound As System.DirectoryServices.SearchResult = ADSearch.FindOne()
Dim propertyCount As Integer = UserFound.Properties("memberOf").Count
If Not IsNothing(UserFound) Then
Dim propertyCounter As Integer
Dim dn As String
For propertyCounter = 0 To propertyCount - 1
dn = CType(UserFound.Properties("memberOf")(propertyCou nter), String)
If InStr(dn.ToString, strGroupName) <> 0 Then
blnIsMemberOfGroup = True
End If
Next
End If
Catch
'* No error processing here. If an error occurs the function will just return FALSE.
End Try
IsMemberOfGroup = blnIsMemberOfGroup
End Function
Best Regards,
Earl Francis