Accessing Active Directory Through the .NET Framework
By Robert Chartier
The System.DirectoryServices namespace gives users access to some rudimentary user administration via ASP.NET. This article first reviews what the Active Directory (AD) is, then looks briefly at the actual System.DirectoryServices namespace itself, and finally presents the code that allows us to add, edit, and delete users.
What Is the Active Directory?
In today's networked environment it's crucial to be able to control access to each network device easily. A method is needed to control who has access to what device and when. This includes devices such as printers, files, and any other local network resource or item on the distributed network. AD provides the ability to do this, integrated with the operating system (OS), which means very intrinsic support at a very low level.
How Does AD Work?
AD is simply a hierarchical, object-orientated database that represents all of your network resources. At the top there's typically the Organization (O), beneath that Organizational Units (OU) as containers, and finally objects that consist of your actual resources. This hierarchical format creates a very familiar and easy-to-administrate tree for systems administrators. For example, if you assign an OU access to a given resource, that access will also be persisted to the objects that are contained within it.
How Can We Access the AD?
Within the .NET Framework we are provided with the System.DirectoryServices namespace, which in turns uses Active Directory Services Interfaces (ADSI). If you have Microsoft Help installed with the .NET Framework Class library, you can refer to the following URL:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDirectoryServices.htm.
If not, take a look on MSDN directly: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDirectoryServices.asp
ADSI is a way to interact with many different directory services providers through code, a programmatic interface. The classes in the System.DirectoryServices namespace can be used with any of the Active Directory service provider listed below:
Within the System.DirectoryServices namespace there are two main classes: the System.DirectoryServices.DirectoryEntry and the System.DirectoryServices.DirectorySearcher classes. This article will not covering these in detail because the information can be read on MSDN using the above URLs. Also, note the DirectorySearcher class only works with the LDAP provider.
Also, when using DirectoryEntry objects, there is a schema for each object. A schema is the type of entry that object is. For example, if you had a DirectoryEntry object with a "User" schema, it would represent a user.
For this article, we will use the Windows 2000 Provider (WinNT://) and the System.DirectoryServices.DirectoryEntry class.
Figure 1.1 AD Service Providers
Directory Service Providers Path Windows NT version 5.0, Windows 2000, or Windows XP WinNT://path Lightweight Directory Access Protocol (LDAP) LDAP://path Novell NetWare Directory Service NDS://path Novell Netware 3.x NWCOMPAT://path Internet Information Services (IIS) IIS://
User Administration
Conclusion
These are the essentials needed to complete a fully functioning DAL for AD for any provider with the System.DirectoryServices namespace. Take this sample, and adapt it to your needs. Keep in mind that for any DAL to be really useful, it should not be limited to a single provider, including your normal database providers. You should be able to swap in a DAL that uses SQL Server to maintain the database of users and remove the AD DAL entirely.
There are a few more portions you may want to complete. This includes adding the functionality for Group administration. This would permit creating, editing, and deleting groups, and listing groups for each user, and listing the users within each group, etc. Unfortunately most of this functionality would need to be done via COM Interop (see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneralconsiderations.asp). For more information on how to do this, refer to the More Info.txt file included in the downloadable ZIP file accompanying this article.
References
System.DirectoryServices namespace from MSDN, see
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDirectoryServices.asp
Active Directory Services Interface (ADSI): Frequently Asked Questions from MSDN, see
http://msdn.microsoft.com/library/en-us/dnactdir/html/msdn_adsifaq.asp
System.Runtime.InteropServices namespace from MSDN, see
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemRuntimeInteropServices.asp
Security Concerns for Visual Basic .NET and Visual C# .NET Programmers (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchSecurityConcernsForVisualBasicNETProgrammers.asp)
VS .NET Security Model (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsent7/html/vxoriDistributedApplicationSecurityRecommendations.asp)
About the Author
Robert Chartier has developed IT solutions for more than nine years with a diverse background in both software and hardware development. He is internationally recognized as an innovative thinker and leading IT architect with frequent speaking engagements showcasing his expertise. He's been an integral part of many open forums on cutting-edge technology, including the .NET Framework and Web Services. His current position as vice president of technology for Santra Technology has allowed him to focus on innovation within the Web Services market space.
He uses expertise with many Microsoft technologies, including .NET, and a strong background in Oracle, BEA Systems, Inc.'s BEA WebLogic, IBM, Java 2 Platform Enterprise Edition (J2EE), and similar technologies to support his award-winning writing. He frequently publishes to many of the leading developer and industry support Web sites and publications. He has a bachelor's degree in Computer Information Systems.

Comments
Developer/Anlyst
Posted by vejee on 04/28/2012 05:02pmHelpful article Thanks! I couldn't download 020731.zip file. can you please send the zip file? Thanks!,
Reply