
Introduction
This guide shows how to create an application in C#, for browsing Active Directory objects. On the left navigate objects/containers with an TreeView control. Only AD root will be read in, after successful authentication. The "children" of the containers will be dynamically loaded after the user clicks on an object. If no "children" exists no, action will be performed. Attributes and their values, of marked containers on the TreeView control, will be displayed on the right ListView control. Both controls a separated with a splitter control. On start an authentication dialog asks user for domain, username, and password.
Ok let´s start!
- Create an empty Windows Forms project
- Add the System.DirectoryServices.dll reference to your project
- Dont forget to include in the code :
using System.DirectoryServices
- In your main form drag a panel from your toolbox on the form
- Set the panel dock property to fill
- Drag an
TreeView control on the panel, name it ctr_tree
- Set the
Treeview dock property to left
- Add a splitter control on the panel
- Set the splitter dock property to left
- Add a
ListView Element on the panel, name it ctr_list
- Set the
ListView property to dock fill
- Set the view property to details
- Add an
EventListener --> AfterSelect to your TreeView Control
- Declare 2 global variables as private:
private DirectoryEntry Base; private string[] str;
In the EventListener of the TreeView control add following code:
if( e.Node.Nodes.Count == 0 ){
DirectoryEntry parent = (DirectoryEntry)e.Node.Tag;
if(parent != null){
if(parent.Children != null){
foreach(DirectoryEntry Iter in parent.Children){
TreeNode childNode = e.Node.Nodes.Add(Iter.Name);
childNode.Tag = Iter;
}
}
}
}
try{
DirectoryEntry list =(DirectoryEntry)e.Node.Tag;
if(list!=null){
ctr_list.Clear();
ctr_list.Columns.Add("Attribute",90,HorizontalAlignment.Left);
ctr_list.Columns.Add("Value",350,HorizontalAlignment.Left);
foreach(object listIter in list.Properties.PropertyNames){
foreach(Object Iter in list.Properties[listIter.ToString()]){
System.Windows.Forms.ListViewItem item =
new System.Windows.Forms.ListViewItem(listIter.ToString(),0);
item.SubItems.Add(Iter.ToString());
ctr_list.Items.AddRange( new ListViewItem[] {item});
}
}
}
}
catch(System.Exception ex){
MessageBox.Show(ex.Message);
}
- Now you can either connect to AD using a dialog which is asking for domain, username, passw. Or connect statically using hardcoded information in the
DirectoryEntry "Connection string". In this example I'm using a connection dialog which is returning an string array of : domain, username, password.
- Now all we need is an
Connect(string[] temp) method. Simply copy the method from the code snippet below and add it to your form: private void Connect(string[] temp){
str=temp;
Base = new DirectoryEntry("LDAP://"+str.GetValue(0).ToString(),
str.GetValue(1).ToString(),str.GetValue(2).ToString());
if(Base != null){
ctr_tree.Nodes.Clear();
ctr_tree.BeginUpdate();
TreeNode childNode = ctr_tree.Nodes.Add(Base.Name);
childNode.Tag = Base;
try{
foreach(DirectoryEntry rootIter in Base.Children){
TreeNode RootNode = childNode.Nodes.Add(rootIter.Name);
RootNode.Tag = rootIter;
}
}
finally{
childNode.Expand();
ctr_tree.EndUpdate();
}
}
}
- Ok lets see how we wire everything together. Let´s take a look at the main:
[STAThread]
static void Main(){
FrmConnect con = new FrmConnect();
if(con.ShowDialog() == DialogResult.OK){
string[] temp=con.returnResults();
Form1 frm = new Form1();
frm.Connect(temp);
Application.Run(frm);
}
}
Ok first we create an instance of the connection dialog before we launch the main frame. After domain, user, passw are submitted we connect and launch the main frame.
Conclusion
That's it!
Iam a student who started developing in C then C++ ( QT/Crossplatfrom ) and now C#.
Besides my studies, I am working at a german Bank in Frankfurt / Main, concerning Active Directory & Database applications.