.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()
        GetUserInfo()
    End Sub
 
    Sub GetUserInfo()
      
        Try
            ‘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
     DirectorySearcher(enTry)
            mySearcher.Filter = “(&(objectClass=user)(anr=smith))”
            Dim resEnt As SearchResult
            Dim rowcomputer As DataRow
            Try
                For Each resEnt In mySearcher.FindAll()
   Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item(“cn”).Value)
   Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item(“distinguishedName”).Value)
   Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item(“name”).Value) 
   Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item(“givenName”).Value)
   Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item(“displayName”).Value)
                Next
            Catch f As Exception
                Console.WriteLine(f.Message)
            End Try
        Catch f As Exception
            Console.WriteLine(f.Message)
        End Try
    End Sub
End Module

4 Comments

  • Thomas Tomiczek said

    Super.
    <br>
    <br>NOW – would you please get rid of the hardcoded LDAP strings?
    <br>
    <br>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.

  • Steve Schofield said

    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.
    <br>' Set objRootDSE = GetObject("LDAP://RootDSE")
    <br>' strConfigurationNC = objRootDSE.Get("configurationNamingContext")
    <br>
    <br>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.

  • http:// said

    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…………. 🙂

  • http:// said

    I want to make something like that but in Web application , not a desktop application

Add a Comment