在园子里找到就翻成VB.net的了。
原文见:
http://www.cnblogs.com/rickie/category/29428.html
对这个类的使用,我会在下篇post中补上
开始要添加对System.DirectoryServices,System.DirectoryServices.Protocols 这两个DLL的引用。
在公共程序集里面都可以找到。
Imports
System.DirectoryServices
Imports
System.Data
Imports
System
Imports
Microsoft.VisualBasic
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public
Class ADHelper
Class ADHelper
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Public Shared ADPath As String = System.Configuration.ConfigurationManager.AppSettings.Get("ADPath")
Public Shared UserName As String
Public Shared PassWord As String
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Enum ADAccountOptionsEnum 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
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Enum LoginResultEnum LoginResult
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LOGIN_OK = 0
LOGIN_USER_DOESNT_EXIST
LOGIN_USER_ACCOUNT_INACTIVE
End Enum
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function IsUserValid()Function IsUserValid(ByVal UserName As String, ByVal PassWord As String) As Boolean
Dim deUser As DirectoryEntry
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function IsAccountActive()Function IsAccountActive(ByVal userAccountControl As Integer) As Boolean
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Function
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function Login()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
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End If
Else
Return LoginResult.LOGIN_USER_DOESNT_EXIST
End If
End Function
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function GetUser()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
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function GetDirectoryObject()Function GetDirectoryObject() As DirectoryEntry
Dim oDe As DirectoryEntry
oDe = New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
Return oDe
End Function
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function GetProperty()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
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function test()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
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Shared Function nopassword()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