Imports System.DirectoryServices

Imports System.Data

Imports System

Imports Microsoft.VisualBasic

Public Class ADHelper
Public Shared ADPath As String = System.Configuration.ConfigurationManager.AppSettings.Get("ADPath")
Public Shared UserName As String
Public Shared PassWord As String
Public Enum ADAccountOptions
UF_TEMP_DUPLICATE_ACCOUNT = &H100
UF_NORMAL_ACCOUNT = &H200
UF_INTERDOMAIN_TRUST_ACCOUNT = &H800
UF_WORKSTATION_TRUST_ACCOUNT = &H1000
UF_SERVER_TRUST_ACCOUNT = &H2000
UF_DONT_EXPIRE_PASSWD = &H10000
UF_SCRIPT = &H1
UF_ACCOUNTDISABLE = &H2
UF_HOMEDIR_REQUIRED = &H8
UF_LOCKOUT = &H10
UF_PASSWD_NOTREQD = &H20
UF_PASSWD_CANT_CHANGE = &H40
UF_ACCOUNT_LOCKOUT = &H10
UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = &H80
End Enum
Public Enum LoginResult
LOGIN_OK = 0
LOGIN_USER_DOESNT_EXIST
LOGIN_USER_ACCOUNT_INACTIVE
End Enum
Public Shared Function IsUserValid(ByVal UserName As String, ByVal PassWord As String) As Boolean
Dim deUser As DirectoryEntry
deUser = New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
Try
Dim native As Object = deUser.NativeObject
Return True
Catch ex As Exception
Return False
Finally
deUser.Close()
End Try
End Function
Public Shared Function IsAccountActive(ByVal userAccountControl As Integer) As Boolean
Dim userAccountControl_Disabled As Integer = Convert.ToInt32(ADAccountOptions.UF_ACCOUNTDISABLE)
Dim flagExists As Integer = userAccountControl And userAccountControl_Disabled
If (flagExists > 0) Then
Return False
Else
Return True
End If
End Function
Public Shared Function Login(ByVal UserName As String, ByVal PassWord As String) As LoginResult
If (IsUserValid(UserName, PassWord)) Then
Dim de As DirectoryEntry = GetUser(UserName)
If (de IsNot DBNull.Value) Then
Dim userAccountControl As Integer = Convert.ToInt32(de.Properties("userAccountControl")(0))
de.Close()
If (Not IsAccountActive(userAccountControl)) Then
Return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE
Else
Return LoginResult.LOGIN_OK
End If
Else
Return LoginResult.LOGIN_USER_DOESNT_EXIST
End If
Else
Return LoginResult.LOGIN_USER_DOESNT_EXIST
End If
End Function
Public Shared Function GetUser(ByVal UserName As String) As DirectoryEntry
Dim de As DirectoryEntry = GetDirectoryObject()
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
de = New DirectoryEntry(results.Path, UserName, PassWord, AuthenticationTypes.Secure)
Return de
Else
Return Nothing
End If
End Function
Public Shared Function GetDirectoryObject() As DirectoryEntry
Dim oDe As DirectoryEntry
oDe = New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
Return oDe
End Function
Public Shared Function GetProperty(ByVal searchResult As SearchResult, ByVal PropertyName As String) As String
If (searchResult.Properties.Contains(PropertyName)) Then
Return searchResult.Properties(PropertyName)(0).ToString
Else
Return String.Empty
End If
End Function
Public Shared Function test(ByVal UserName As String) As SearchResult
Dim de As DirectoryEntry = GetDirectoryObject()
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
Return results
Else
Return Nothing
End If
End Function
Public Shared Function nopassword(ByVal UserName As String) As SearchResult
Dim de As DirectoryEntry = New DirectoryEntry(ADPath)
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
Return results
Else
Return Nothing
End If
End Function
End Class
