.NET sample LDAP query looking for a specific user name of (smith) and System.DirectoryServices namespace

Here is a sample query that would search for a specific user.  If you want an *GUI* tool to view LDAP and get the correct LDAP path, use ADSIEdit.msc  Its part of the support tools on the w2k/wk2k3 cd.  look for suptools.msi.  this will install some stuff, look for adsiedit.msc once installed.  Once ADSIEdit.msc is opened up.  Look under the domain partition, this is where all user info stuff would be stored.  (be careful in using this tool) on a production Domain Controller however

Imports System.DirectoryServices
Module Module1
    Sub Main()
    End Sub


    Sub GetUserInfo()
            ‘This is a LDAP path to a specific domain controller for LDAP
            ‘Dim enTry As DirectoryEntry = New DirectoryEntry(“LDAP://DC1/OU=MyUsers,DC=Steve,DC=Schofield,DC=com“)
            ‘This is a generic LDAP call, it would do a DNS lookup to find a DC in your AD site, scales better
            Dim enTry As DirectoryEntry = New DirectoryEntry(“LDAP://OU=MyUsers,DC=Steve,DC=Schofield,DC=com“)


            Dim mySearcher As DirectorySearcher = New
            mySearcher.Filter = “(&(objectClass=user)(anr=smith))”
            Dim resEnt As SearchResult
            Dim rowcomputer As DataRow
                For Each resEnt In mySearcher.FindAll()
            Catch f As Exception
            End Try
        Catch f As Exception
        End Try
    End Sub
End Module

4 thoughts on “.NET sample LDAP query looking for a specific user name of (smith) and System.DirectoryServices namespace”

  1. Super.

    NOW – would you please get rid of the hardcoded LDAP strings?

    Guess what, there is a known object you can ask to retrieve the AD servers of your domain. No need to have ANYTHING hardcoded. Not even the domain name.


  2. i will post up a .NET version of using the ROOTDSE example later. I used to use vbscript but have been converting stuff over to .NET.
    ‘ Set objRootDSE = GetObject("LDAP://RootDSE")
    ‘ strConfigurationNC = objRootDSE.Get("configurationNamingContext")

    Because I have the requirement of crossing multiple AD forests, its more efficient to pass in as an arguement to the console app or service vs working in a single domain using the above code.


  3. Its not soo clear. If i get a complete application which uses LDAP to authenticate a user using c#.net might help me a better…………. 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: