Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

AD console project in C# in VS 2008

0.00/5 (No votes)
7 Sep 2008 1  
Getting Active Directory queries to work in ASP.NET and IIS can be extremely tricky, especially if you're just getting started with ASP.NET, IIS and

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Getting Active Directory queries to work in ASP.NET and IIS can be extremely tricky, especially if you're just getting started with ASP.NET, IIS and the DirectoryServices namespace.  It's often helpful to get your basic queries working in a Console App first and then move the working code into the ASP.NET app once it's doing what you need it to do.

How to set up a C# console project in VS 2008 to access Active Directory

  1. Open Microsoft® Visual Studio® 2008.
  2. From the File menu, select New and then Project....
  3. After a few seconds, the New Project Window appears.
  4. In the Project types pane to the left, expand Visual C# and select Windows.
  5. In the Templates pane to the right, in Visual Studio installed templates, select Console Application.
  6. In the upper right section of the New Project window (new for VS 2008) is a drop-down where you can select the version of the .NET Framework against which the project will be built.  For this demonstration, we'll be using .NET Framework 3.5.
  7. Give the project a name and select its location.  I recommend ticking the "Create directory for solution" box as this makes it easier to add additional projects such as Class Libraries and Unit Test projects.
  8. Click OK.

This completes the basic set up of the project but as yet we don't have access to any of the AD/LDAP classes because they're in libraries that aren't included by default.  In .NET 3.5, there are three libraries, System.DirectoryServices (wrappers round ADSI objects), System.DirectoryServices.Protocols (which uses the Windows LDAP library (wldap32.dll)) and System.DirectoryServices.AccountManagement (ADSI, again, but focused on account management).  For this demonstration, we'll only use System.DirectoryServices.

How to add a library to your project

  1. In Visual Studio, open the Solution Explorer (from the View menu, select Solution Explorer).
  2. In Solution Explorer, select the project (not the Solution) or one of the project's files.  This will add extra items to the Project menu.
  3. From the Project menu, select Add Reference....
  4. After a few seconds, the Add Reference window appears.
  5. In the .NET tab, scroll down to System.DirectoryServices, select it and click OK.  (At this point, you could select several libraries but we only need this one).

This makes the library available to classes in the project.  Unlike with VB, there's no way to import the namespace(s) to every code file automatically.

How to import the namespace

At this point, all of the classes in System.DirectoryServices are available in your code files.  However, you have to use the full namespace name such as System.DirectoryServices.DirectorySearcher.  In order to be able to refer to a class by its name only, import the namespace by adding this command at the top of each code file:

using System.DirectoryServices; 

To test the project, add the following code to :

string defaultNamingContext;
using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"))
{
    defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
}
Console.WriteLine("Accessing domain: {0}", defaultNamingContext);

Console.WriteLine("\r\nPress a key to continue...");
Console.ReadKey(true);

and hit F5 to compile and run.  That's it.

Good luck with your directory services programming.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here