Click here to Skip to main content
13,148,573 members (56,747 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 26 Jul 2010

Performing Queries Against Active Directory Domain Services

, 26 Jul 2010
Rate this:
Please Sign up or sign in to vote.
This tip explains how to perform queries against Active Directory Domain Services

One of the missions that needed my attention lately was to check whether a user exists in an enterprise Active Directory.
The article will show exactly how to perform such a query.

The DirectoryEntry Class

The DirectoryEntry class represent an entry in Active Directory.
That entry live in memory when created and changes that you perform on it won’t be submitted to Active Directory unless you call the CommitChnages method. That class can be found in System.DirectoryServices namespace.
The following code shows how to create a DirectoryEntry object using a the path to the LDAP, username and password:

var entry = new DirectoryEntry(path, username, password);

The DirectorySearcher Class

The DirectorySearcher class enable us to perform queries against Active Directory. Once you have a DirectoryEntry in hand you can pass it to the DirectorySearcher and then commit queries to your Active Directory. When you create the DirectorySearcher you also supply the optional list of properties that you want to retrieve. As the DirectoryEntry, it is also available in the System.DirectoryServices namespace.
The following code shows how to create a DirectorySearcher with a given DirectoryEntry:

var searcher = new DirectorySearcher(entry);

How to Perform a Query Against Active Directory Domain Service

The following code snippet shows a simple method that perform a query against Active Directory:

private SearchResult SearchLDAPById(string userId, string path, string username, string password)
    var entry = new DirectoryEntry(path, username, password);
    var search = new DirectorySearcher(entry);
    search.Filter = string.Format("({0}={1})", "SAMAccountName", userId);
    return search.FindOne();

The query returns the display name for a logon name of a user which is saved in Active Directory as SAMAccountName. As you can see we get back a SearchResult object which we can investigate for our retrieved display name.


In the article I showed how you can perform a query against Active Directory domain service. I also introduced the DirectoryEntry and DirectorySearcher classes. I hope you will find this information useful.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 27 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid