protected void Page_Load(object sender, EventArgs e)
{
DirectoryEntry de = new DirectoryEntry(ldap://host:389/baseDN, admin, pwd);
TreeNode root = new TreeNode("root", "root");
TreeNode tn;
foreach (DirectoryEntry child in de.Children) //循环遍历根目录下的OU
{
if (child.Name.Contains("OU"))
{
string ou = child.Name.Split(new char[] { '=' })[1].ToString();
string url = child.Properties["distinguishedName"].Value.ToString();
DirectoryEntry dir = new DirectoryEntry(ldapIP+url,admin, pwd);
tn = new TreeNode();
tn.Text = ou;
this.treeView(dir, tn);
root.ChildNodes.Add(tn);
}
}
this.TreeView1.Nodes.Add(root);
}
public void treeView(DirectoryEntry de,TreeNode root) //遍历每个根目录OU中的OU
{
foreach (DirectoryEntry child in de.Children)
{
if (child.Name.Contains("OU"))
{
string ou = child.Name.Split(new char[] { '=' })[1].ToString();
string url = child.Properties["distinguishedName"].Value.ToString();
DirectoryEntry dir = new DirectoryEntry(ldapIP+url, admin, pwd);
TreeNode tree = new TreeNode();
tree.Text = ou;
//递归
this.treeView(dir, tree);
root.ChildNodes.Add(tree);
}
else
{
TreeNode leaf = new TreeNode();
if (child.Properties["displayName"].Value != null)
{
leaf.Text = child.Properties["displayName"].Value.ToString();
if (child.Properties["mobile"].Value != null)
{
leaf.Value = child.Properties["mobile"].Value.ToString();
}
else
{
leaf.Value = "0";
}
root.ChildNodes.Add(leaf);
}
}
}
}