AD活动目录操作类(VB.net)

在园子里找到就翻成VB.net的了。
原文见:
http://www.cnblogs.com/rickie/category/29428.html
对这个类的使用,我会在下篇post中补上
开始要添加对System.DirectoryServices,System.DirectoryServices.Protocols 这两个DLL的引用。
在公共程序集里面都可以找到。
None.gif Imports  System.DirectoryServices
None.gif
Imports  System.Data
None.gif
Imports  System
None.gif
Imports  Microsoft.VisualBasic
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Class ADHelper Class ADHelper
InBlock.gif
InBlock.gif    
Public Shared ADPath As String = System.Configuration.ConfigurationManager.AppSettings.Get("ADPath")
InBlock.gif    
Public Shared UserName As String
InBlock.gif    
Public Shared PassWord As String
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Enum ADAccountOptionsEnum ADAccountOptions
InBlock.gif        UF_TEMP_DUPLICATE_ACCOUNT 
= &H100
InBlock.gif        UF_NORMAL_ACCOUNT 
= &H200
InBlock.gif        UF_INTERDOMAIN_TRUST_ACCOUNT 
= &H800
InBlock.gif        UF_WORKSTATION_TRUST_ACCOUNT 
= &H1000
InBlock.gif        UF_SERVER_TRUST_ACCOUNT 
= &H2000
InBlock.gif        UF_DONT_EXPIRE_PASSWD 
= &H10000
InBlock.gif        UF_SCRIPT 
= &H1
InBlock.gif        UF_ACCOUNTDISABLE 
= &H2
InBlock.gif        UF_HOMEDIR_REQUIRED 
= &H8
InBlock.gif        UF_LOCKOUT 
= &H10
InBlock.gif        UF_PASSWD_NOTREQD 
= &H20
InBlock.gif        UF_PASSWD_CANT_CHANGE 
= &H40
InBlock.gif        UF_ACCOUNT_LOCKOUT 
= &H10
InBlock.gif        UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED 
= &H80
ExpandedSubBlockEnd.gif    
End Enum

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Enum LoginResultEnum LoginResult
InBlock.gif
InBlock.gif        LOGIN_OK 
= 0
InBlock.gif        LOGIN_USER_DOESNT_EXIST
InBlock.gif        LOGIN_USER_ACCOUNT_INACTIVE
ExpandedSubBlockEnd.gif    
End Enum

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function IsUserValid()Function IsUserValid(ByVal UserName As StringByVal PassWord As StringAs Boolean
InBlock.gif        
Dim deUser As DirectoryEntry
InBlock.gif
InBlock.gif        deUser 
= New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
InBlock.gif        
Try
InBlock.gif            
Dim native As Object = deUser.NativeObject
InBlock.gif            
Return True
InBlock.gif        
Catch ex As Exception
InBlock.gif            
Return False
InBlock.gif        
Finally
InBlock.gif            deUser.Close()
InBlock.gif        
End Try
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function IsAccountActive()Function IsAccountActive(ByVal userAccountControl As IntegerAs Boolean
InBlock.gif
InBlock.gif        
Dim userAccountControl_Disabled As Integer = Convert.ToInt32(ADAccountOptions.UF_ACCOUNTDISABLE)
InBlock.gif        
Dim flagExists As Integer = userAccountControl And userAccountControl_Disabled
InBlock.gif        
If (flagExists > 0Then
InBlock.gif            
Return False
InBlock.gif        
Else
InBlock.gif            
Return True
InBlock.gif        
End If
InBlock.gif
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function Login()Function Login(ByVal UserName As StringByVal PassWord As StringAs LoginResult
InBlock.gif        
If (IsUserValid(UserName, PassWord)) Then
InBlock.gif            
Dim de As DirectoryEntry = GetUser(UserName)
InBlock.gif            
If (de IsNot DBNull.Value) Then
InBlock.gif                
Dim userAccountControl As Integer = Convert.ToInt32(de.Properties("userAccountControl")(0))
InBlock.gif                de.Close()
InBlock.gif                
If (Not IsAccountActive(userAccountControl)) Then
InBlock.gif                    
Return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE
InBlock.gif                
Else
InBlock.gif                    
Return LoginResult.LOGIN_OK
InBlock.gif                
End If
InBlock.gif            
Else
InBlock.gif                
Return LoginResult.LOGIN_USER_DOESNT_EXIST
InBlock.gif
InBlock.gif            
End If
InBlock.gif        
Else
InBlock.gif            
Return LoginResult.LOGIN_USER_DOESNT_EXIST
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function GetUser()Function GetUser(ByVal UserName As StringAs DirectoryEntry
InBlock.gif        
Dim de As DirectoryEntry = GetDirectoryObject()
InBlock.gif        
Dim deSearch As New DirectorySearcher
InBlock.gif        deSearch.SearchRoot 
= de
InBlock.gif        deSearch.Filter 
= "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
InBlock.gif        deSearch.SearchScope 
= SearchScope.Subtree
InBlock.gif        
Dim results As SearchResult = deSearch.FindOne
InBlock.gif        
If (results IsNot DBNull.Value) Then
InBlock.gif            de 
= New DirectoryEntry(results.Path, UserName, PassWord, AuthenticationTypes.Secure)
InBlock.gif            
Return de
InBlock.gif        
Else
InBlock.gif            
Return Nothing
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function GetDirectoryObject()Function GetDirectoryObject() As DirectoryEntry
InBlock.gif        
Dim oDe As DirectoryEntry
InBlock.gif        oDe 
= New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
InBlock.gif        
Return oDe
ExpandedSubBlockEnd.gif    
End Function

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function GetProperty()Function GetProperty(ByVal searchResult As SearchResult, ByVal PropertyName As StringAs String
InBlock.gif        
If (searchResult.Properties.Contains(PropertyName)) Then
InBlock.gif            
Return searchResult.Properties(PropertyName)(0).ToString
InBlock.gif        
Else
InBlock.gif            
Return String.Empty
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Function

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function test()Function test(ByVal UserName As StringAs SearchResult
InBlock.gif        
Dim de As DirectoryEntry = GetDirectoryObject()
InBlock.gif        
Dim deSearch As New DirectorySearcher
InBlock.gif        deSearch.SearchRoot 
= de
InBlock.gif        deSearch.Filter 
= "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
InBlock.gif        deSearch.SearchScope 
= SearchScope.Subtree
InBlock.gif        
Dim results As SearchResult = deSearch.FindOne
InBlock.gif        
If (results IsNot DBNull.Value) Then
InBlock.gif            
Return results
InBlock.gif        
Else
InBlock.gif            
Return Nothing
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Function

ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Shared Function nopassword()Function nopassword(ByVal UserName As StringAs SearchResult
InBlock.gif        
Dim de As DirectoryEntry = New DirectoryEntry(ADPath)
InBlock.gif        
Dim deSearch As New DirectorySearcher
InBlock.gif        deSearch.SearchRoot 
= de
InBlock.gif        deSearch.Filter 
= "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
InBlock.gif        deSearch.SearchScope 
= SearchScope.Subtree
InBlock.gif        
Dim results As SearchResult = deSearch.FindOne
InBlock.gif        
If (results IsNot DBNull.Value) Then
InBlock.gif            
Return results
InBlock.gif        
Else
InBlock.gif            
Return Nothing
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Function

ExpandedBlockEnd.gif
End Class

None.gif


转载于:https://www.cnblogs.com/yangboshan/archive/2007/07/25/830747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值