Active Directory in Treeview in ASp.net with CheckBox
This is an example of how to build an ASP.Net LDAP
The Sample project is having all the Functionality like load all the directory details to Treeview on pageload and also dynamically load LADP using user given details like LADP Path,username,passowrd.
below I am going to give some important Functionality only. so download project and Go through all the Functionality .
variable declaraiton
hdnAdPath.Value = ConfigurationManager.AppSettings["AdPathRoot"].ToString();
hdnUserName.Value = ConfigurationManager.AppSettings["UserName"].ToString();
hdnUserPassword.Value = ConfigurationManager.AppSettings["Password"].ToString();
tvDomainController.Visible = false;
lblStatus.Visible = false;
First Step 1:
In Page load event check whether system in domain control or not
protected void CheckSystemLevel()
{
// Get the Domain Name
String DomainName = Environment.UserDomainName.ToString();
// Get Domain Server name
String domainserverName = Environment.GetEnvironmentVariable("LOGONSERVER");
// Get Local System Name
string localSystemName = Environment.MachineName.ToString();
// Check whether system in domain control or not
if (!domainserverName.Contains(localSystemName) && DomainName != localSystemName)
{
// Client Machine
// System is connection to Domain Server
hdnStatus.Value = "false";
LoadDomainName();
}
else if (DomainName == localSystemName && domainserverName.Contains(localSystemName))
{
// Local System ???
// System is not having domaincontroller
hdnStatus.Value= "true";
LoadDomainDetailswithPath();
}
else if (domainserverName.Contains(localSystemName) && DomainName != localSystemName)
{
// Domain Controller System
// server System
hdnStatus.Value = "false";
LoadDomainName();
}
}
Step 2:
Load Root node in tree view
protected void LoadDomainName()
{
try
{
DirectoryEntry adRootDSE = new DirectoryEntry(_adrootPath);
DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + (string)adRootDSE.Properties["defaultNamingContext"].Value);
TreeNode tnRoot = new TreeNode();
tnRoot.Text = adRoot.Properties["Name"].Value.ToString();
tnRoot.Value = adRoot.Path.ToString().Replace("/", "\\");
tnRoot.SelectAction = TreeNodeSelectAction.Expand;
tnRoot.PopulateOnDemand = true;
tnRoot.ShowCheckBox = false;
tnRoot.ImageUrl = adImageURL[(int)AdImages.AdRoot];
tvDomainController.Nodes.Clear();
tvDomainController.Nodes.Add(tnRoot);
tvDomainController.Visible = true;
lblStatus.Visible = false;
}
catch (Exception ex)
{
lblStatus.Text = "Directory expecting username and password";
lblStatus.Visible = true;
tvDomainController.Visible = false;
}
}
STEP 3:
Load all the directorys in treeview using following code
protected void PopulateNode(Object source, TreeNodeEventArgs e)
{
if (e.Node.Value != string.Empty)
{
DirectoryEntry selectedEntry;
if (hdnStatus.Value == "false" )
{
selectedEntry = new DirectoryEntry(e.Node.Value.Replace("\\", "/"));
}
else
{
selectedEntry = new DirectoryEntry(e.Node.Value.Replace("\\", "/"), hdnUserName.Value, hdnUserPassword.Value);
}
if (DirectoryEntry.Exists(selectedEntry.Path))
{
foreach (DirectoryEntry child in selectedEntry.Children)
{
TreeNode tvNode = e.Node;
TreeNode tmpNode = new TreeNode();
switch (child.SchemaClassName)
{
case "organizationalUnit":
tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Ou];
tvNode.ChildNodes.Add(tmpNode);
break;
case "container":
tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Container];
tvNode.ChildNodes.Add(tmpNode);
break;
case "computer":
tmpNode.Text = child.Properties["Name"].Value.ToString() + child.SchemaClassName.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Computer];
tvNode.ChildNodes.Add(tmpNode);
break;
case "user":
tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.None;
tmpNode.PopulateOnDemand = false;
tmpNode.ImageUrl = adImageURL[(int)AdImages.User];
tvNode.ChildNodes.Add(tmpNode);
break;
case "group":
tmpNode.Text = child.Properties["Name"].Value.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.None;
tmpNode.PopulateOnDemand = false;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Group];
tvNode.ChildNodes.Add(tmpNode);
break;
default:
tmpNode.Text = child.Properties["Name"].Value.ToString() + child.SchemaClassName.ToString();
tmpNode.Value = child.Path.ToString().Replace("/", "\\");
tmpNode.SelectAction = TreeNodeSelectAction.Expand;
tmpNode.PopulateOnDemand = true;
tmpNode.ImageUrl = adImageURL[(int)AdImages.Unknown];
tvNode.ChildNodes.Add(tmpNode);
break;
}
}
}
}
}
Step 4
Find the node using Value path
// Find the node specified by the user.
TreeNode node = tvDomainController.FindNode(Server.HtmlEncode(txtPath.Text));
if (node != null)
{
// Indicate that the node was found.
Message.Text = "The specified node (" + node.ValuePath + ") was found.";
node.Checked = true;
}
else
{
// Indicate that the node is not in the TreeView control.
Message.Text = "The specified node (" + txtPath.Text + ") is not in this TreeView control.";
}