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
- Open Microsoft® Visual Studio® 2008.
- From the File menu, select New and then Project....
- After a few seconds, the New Project Window appears.
- In the Project types pane to the left, expand Visual C# and select Windows.
- In the Templates pane to the right, in Visual Studio installed templates, select Console Application.
- 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.
- 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.
- 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
- In Visual Studio, open the Solution Explorer (from the View menu, select Solution Explorer).
- 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.
- From the Project menu, select Add Reference....
- After a few seconds, the Add Reference window appears.
- 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.