Feb. 15, 2011, 10:10 p.m.
posted by trystan
Programming with .NET
The System.DirectoryServices namespace can be used to interface with Active Directory using the .NET Framework. The following code is a simple VB.NET program that prints the attributes of the RootDSE:
Imports System.DirectoryServices Module Module1 Sub Main( ) Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE") Dim strAttrName As String Dim objValue As Object For Each strAttrName In objRootDSE.Properties.PropertyNames For Each objValue In objRootDSE.Properties(strAttrName) Console.WriteLine(strAttrName & " : " & objValue.ToString) Next objValue Next strAttrName End Sub End Module
The System.DirectoryServices DirectorySearcher class is a simple interface for making LDAP queries. The DirectoryEntry class is used for instantiating existing objects or creating new ones. In the Solution section, we used the DirectoryEntry class to access the RootDSE. DirectorySearcher and DirectoryEntry are the two main classes to become familiar with if you want to do Active Directory programming with .NET. For more information and examples on using System.DirectoryServices, see Chapter 31 of Active Directory, Third Edition, by Joe Richards et al. (O'Reilly).
System.DirectoryServices does not currently provide interfaces for everything that could be done with ADSI. Instead, you can use the NativeObject property on an instantiated object to return an ADSI object, which you can then use to access any ADSI properties or methods for the object.
Chapter 31 in Active Directory, Third Edition, by Joe Richards et al. (O'Reilly) and reference information for System.DirectoryServices at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDirectoryServices.asp