Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Article

AD console project in C# in VS 2008

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL2 min read 8.6K   2  
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; <br />

To test the project, add the following code to :

string defaultNamingContext;<br />using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"))<br />{<br />    defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();<br />}<br />Console.WriteLine("Accessing domain: {0}", defaultNamingContext);<br /><br />Console.WriteLine("\r\nPress a key to continue...");<br />Console.ReadKey(true); <br />

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

Good luck with your directory services programming.

 

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --