Question Problem with DirectorySearcher

abhik7188

New member
Joined
Aug 18, 2010
Messages
1
Programming Experience
Beginner
Hi All,
I am developing a code in vb.net to check users from Active Directory. I am able search most of the records by Samaccountname but for few records, it's not able to search. But I am able to search those records with same filter from LDAP browser. I have tried different filters also for those records, but no luck. Could you please review the code below and let me know your suggestion.

Imports System.DirectoryServices

Public Class Form1
Private Shared de As DirectoryEntry
Dim deSearch As DirectorySearcher
Dim lst As ArrayList

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ret As String = ""
Dim sr As SearchResult
Dim strSAMACCTname As String
Dim results As SearchResultCollection

strSAMACCTname = "ACHATER10"
de = New DirectoryEntry()
de.Path = "LDAP://ldap.xyz.com"

de.Username = "myname"
de.Password = "mypassword"

deSearch = New DirectorySearcher


deSearch.Filter = "(&(objectClass=user)(sAMAccountName=" & strSAMACCTname & "))"

deSearch.PropertiesToLoad.Add("userAccountControl")
deSearch.PropertiesToLoad.Add("sAMAccountName")

deSearch.SearchScope = SearchScope.Subtree

results = deSearch.FindAll

If results.Count = 0 Then
ret = "NOT PRESENT"
Else
For Each sr In results
If sr.GetDirectoryEntry().Properties("userAccountControl").Value = "512" Then
ret = "ACTIVE"
ElseIf sr.GetDirectoryEntry().Properties("userAccountControl").Value = "514" Then
ret = "INACTIVE"
End If
Next
End If

If ret <> "ACTIVE" Then MsgBox(strSAMACCTname & " not found in LDAP")
MsgBox(ret)
End Sub
End Class
 
Back
Top