Here is a simple class I picked up
Imports System.DirectoryServices
Public Class RoleUser
Private _DisplayName As String = String.Empty
Private _Username As String = String.Empty
Private _Groups() As String
Private _IsAuthenticated As Boolean = False
Private _Domain As String = Environment.UserDomainName
Public ReadOnly Property DisplayName() As String
Get
Return _DisplayName
End Get
End Property
Public ReadOnly Property Username() As String
Get
Return _Username
End Get
End Property
Public ReadOnly Property Groups() As String()
Get
Return _Groups
End Get
End Property
Public ReadOnly Property IsAuthenticated() As Boolean
Get
Return _IsAuthenticated
End Get
End Property
Public Function Authenticate(ByVal username As String, ByVal password As String) As Boolean
Try
If username Is Nothing Then username = String.Empty
If password Is Nothing Then password = String.Empty
Dim de As New DirectoryEntry("LDAP://" & _Domain, username, password, AuthenticationTypes.Secure)
Dim srch As New DirectorySearcher(de)
srch.Filter = "(&(objectClass=user)(samAccountName=" & username & "))"
Dim results As SearchResultCollection = srch.FindAll
Dim res As SearchResult
Dim al As New ArrayList
For Each res In results
Dim obj As Object
For Each obj In res.Properties("MemberOf")
al.Add(TrimToName(obj))
Next
Next
_Groups = al.ToArray(GetType(String))
_Username = res.Properties("samAccountName")(0)
_DisplayName = res.Properties("displayname")(0)
_IsAuthenticated = True
Catch ex As Exception
MsgBox(ex.Message & ControlChars.NewLine & ex.StackTrace, MsgBoxStyle.Critical, TypeName(ex))
Dim grp() As String
_Groups = grp
_Username = String.Empty
_DisplayName = String.Empty
_IsAuthenticated = False
End Try
Return _IsAuthenticated
End Function
Public Function IsInGroup(ByVal group As String) As Boolean
If _Groups.IndexOf(_Groups, group) > -1 Then Return True
End Function
Public Sub New()
'default
End Sub
Public Sub New(ByVal username As String, ByVal password As String)
Authenticate(username, password)
End Sub
Public Sub New(ByVal domain As String)
_Domain = domain
End Sub
Private Function TrimToName(ByVal path As String) As String
Dim parts() As String = path.Split(",")
Return parts(0).Replace("CN=", String.Empty)
End Function
End Class
roleuser.zip (987 Bytes)