April 11, 2014 | Posted in: .Net

DirectorySearcher.FindAll() is very slow. Instead search for the specific AD user, much faster:

Dim windowsLogin As String
Dim currentIdentity As System.Security.Principal.WindowsIdentity
Dim slashIndex As Integer = 0

currentIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
slashIndex = currentIdentity.Name.IndexOf(“\”)
windowsLogin = currentIdentity.Name.Substring(slashIndex + 1)
End Try

Dim searcher As New DirectorySearcher(“”)
Dim result As SearchResult = Nothing
With searcher
.Filter = String.Format(“(&(objectCategory=person)(SAMAccountName={0}))”, windowsLogin)
result = .FindOne()
End With

If ((result IsNot Nothing) AndAlso
(result.Properties.Item(“SAMAccountName”) IsNot Nothing) AndAlso
(result.Properties.Item(“SAMAccountName”).Item(0) IsNot Nothing) AndAlso
(CStr(result.Properties.Item(“SAMAccountName”).Item(0)).ToUpper() = windowsLogin.ToUpper())) Then

MessageBox.Show(String.Format(“User {0}”, windowsLogin) + ” found”)

End If

Be the first to comment.

Leave a Reply


fourteen − 9 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>