Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Fine-tune Your Active Directory Operations with .NET : Page 3

Accessing and modifying objects in Active Directory from .NET languages is a straightforward process, thanks to built-in support in the .NET framework.


advertisement
Modifying Objects in Active Directory
You can update objects in the repository as shown below:

DirectoryEntry directoryEntry= new DirectoryEntry( "LDAP://192.168.1.49/CN=Users;DC=DomainName"); if(de.Properties.Contains("city")) { de.Properties["city"][0] = "Hyderabad"; de.CommitChanges(); }

Deleting an object in the Active Directory
The following code snippet shows how we can delete an object in the Active Directory.

DirectoryEntry directoryEntry = new DirectoryEntry( strPath,strUserName, strPassword); DirectoryEntry user = directoryEntry.Children.Find( "CN=UserName", "User"); directoryEntry.Children.Remove(user); directoryEntry.CommitChanges(); directoryEntry.Close();

Using Active Directory in C#
This section discusses the implementation of a simple Windows application that displays the names of all the groups from the Active Directory. It also displays the list of all the users of a particular group in the Active Directory. Figure 1 shows a screen shot of the application at runtime.

 
Figure 1. Sample Application: This application retrieves a list of all users in a selected group from Active Directory.
You can download the source code for this sample application to follow along or try it yourself. The two important files in the downloadable source the Windows Form Form1.cs, and a C# file, ADUtilities.cs, which defines a class containing methods to query Active Directory and return results. Here's the code for the ADUtilities class:



public class ADUtilities { Private DirectoryEntry activeDirectory = null; public ADUtilities() { activeDirectory = new System.DirectoryServices.DirectoryEntry( "WinNT://"+Environment.MachineName+",computer"); } public DirectoryEntry IsValidUser(string userName) { return activeDirectory.Children.Find(userName, "User"); } public string GetUserDomain(string userName) { DirectoryEntry user = IsValidUser(userName); if(user != null) return user.Path; return null; } public ArrayList GetGroupNames() { ArrayList groupNames = new ArrayList(); foreach (DirectoryEntry directoryEntry in activeDirectory.Children) { if (directoryEntry.SchemaClassName.Equals("Group")) groupNames.Add(directoryEntry); } return groupNames; } public ArrayList GetUsersInGroup(DirectoryEntry group) { ArrayList groupUsers = new ArrayList(); foreach (DirectoryEntry directoryEntry in group.Children) { if (directoryEntry.SchemaClassName.Equals("User")) groupUsers.Add(directoryEntry); } return groupUsers; } }

The ADUtilities class constructor connects to Active Directory by instantiating a DirectoryEntry class instance, which it then uses in the various methods to retrieve the group names (GetGroupNames) and a list of all the users in a particular group (GetUsersInGroup) in the Active Directory. Note that the Path property of the DirectoryEntry class returns the domain name to which a user belongs.

The simplicity with which you can use .NET code and ADSI to access, update, and search Active Directory (and other directory stores as well) adds to the attractiveness of using directory services within organizations that use .NET. In short, Active Directory is a centralized, secure repository of organizational resources and security information, that's easily accessible via .NET code, which can greatly simplify enterprise-level resource management.



Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap