Search Active Directory for a user

I wish to search Active Directory for a specific user. At the moment, I have code which searches for all users for the domain. This works fine, and is listed below. I can step through the resutls and match the user, but its a bit slow.
I'd like to change the filter to search for a specific logon, but I can't seem to get it. Actually, there's two parts to my question:
1) What should mySearcher.Filter be set to, if I want to search for a logon.
2) I want to get the Company and Office from the found user. For example, to find the Email, I'm using:
               If resEnt.Properties.Contains("mail") Then
                  rowUser.Email = resEnt.Properties("mail").Item(0)
               End If
How can I find out what Company and Office are called in code - is there a list somewhere?

      Const USER_FILTER As String = "(objectClass=Person)"
      Const My_LDAP As String = "LDAP://My_Domain"

      Dim enTry As DirectoryEntry = New DirectoryEntry(MY_LDAP)
      Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
      mySearcher.Filter = USER_FILTER

      Dim resEnt As SearchResult
      For Each resEnt In mySearcher.FindAll()
Who is Participating?
vs1784Connect With a Mentor Commented:
I use this class for all my AD searches. It works for all type of searches i need.

May be you can use it too.

jdhackettAuthor Commented:
Thanks. I see I have to register to download.
Btw, that is a C# project - is it much work to get it working with VB2005?
Which method you want to use?

Either you can use the same class without converting it to VB.NET. As you can have classes programmed in different languages in Visual Studio. Or if you want to convert it on the fly you can go to below linnk to convert it fast.

Hope it helps

jdhackettAuthor Commented:
Figured out how to do it:
  Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
      mySearcher.Filter = "(&(objectClass=person)(sAMAccountName=" & UserName & "))"

Just needed so search by sAMAccountName.
Thanks for the help though, I'm sure those helper classes will come in very useful.
All Courses

From novice to tech pro — start learning today.