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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.