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

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 StringByVal PassWord As StringAs 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 IntegerAs Boolean

        
Dim userAccountControl_Disabled As Integer = Convert.ToInt32(ADAccountOptions.UF_ACCOUNTDISABLE)
        
Dim flagExists As Integer = userAccountControl And userAccountControl_Disabled
        
If (flagExists > 0Then
            
Return False
        
Else
            
Return True
        
End If

    
End Function


    
Public Shared Function Login(ByVal UserName As StringByVal PassWord As StringAs 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 StringAs 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 StringAs 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 StringAs 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 StringAs 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值