65.9K
CodeProject 正在变化。 阅读更多。
Home

Active Directory 搜索器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.69/5 (8投票s)

2005年6月17日

CPOL
viewsIcon

80476

downloadIcon

2801

如何搜索 Active Directory。

引言

这个工具是为了在Active Directory中搜索用户信息而创建的。但它尚未完成。 欢迎就此主题提出任何意见和建议。

这里是截图和部分代码(请参阅源代码中的ActiveDirectorySearcher.cs文件),其中包含应用程序的所有功能。

Sample Image - ADSScreenShot.png

using System;
using System.DirectoryServices;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections;

namespace Ads
{
    /// <summary>
    /// Represents main Active Directory Searcher functionality.
    /// </summary>
    [DefaultProperty("Domain")]
    public class ActiveDirectorySearcher : DirectorySearcher
    {
        private string strDomain;
        private string strAccount;
        private string strPassword;
        private AuthenticationTypes atAuthType;
        private int intNestedLevelCount;

        /// <summary>
        /// Creates and initializes new class instance.
        /// </summary>
        public ActiveDirectorySearcher()
        {
            base.Filter = "(objectCategory=computer)";
            intNestedLevelCount = -1;
        }

        /// <summary>
        /// Gets the node in the Active Directory hierarchy where the search starts.
        /// </summary>
        [Description("Path to search")]
        [Category("System")]
        [ReadOnly(true)]
        public new DirectoryEntry SearchRoot
        {
            get { return new DirectoryEntry(String.Format("LDAP://{0}", 
                  strDomain), String.Format("{0}/{1}", strDomain, strAccount), 
                  strPassword, atAuthType); }
        }

        /// <summary>
        /// Sets the node in the Active Directory hierarchy where
        /// the search starts depending on the values
        /// of <see cref="Domain"/>, etc.
        /// </summary>
        private void SetSearchRoot()
        {
            if((strAccount != null)&&(strAccount.Trim() != String.Empty))
                base.SearchRoot = new DirectoryEntry(String.Format("LDAP://{0}", 
                                  strDomain), String.Format("{0}/{1}", 
                                  strDomain, strAccount), strPassword, atAuthType);
            else
                base.SearchRoot = new DirectoryEntry(String.Format("LDAP://{0}", 
                                                                    strDomain));
        }

        /// <summary>
        /// Domain to search name.
        /// </summary>
        [Description("Domain to search")]
        [Category("Domain Definition")]
        public string Domain
        {
            get { return strDomain; }
            set
            {
                strDomain = value;
                SetSearchRoot();
            }
        }

        /// <summary>
        /// Gets or sets the type of authentication to use.
        /// </summary>
        [DefaultValue(AuthenticationTypes.None)]
        [Description("Authentication type")]
        [Category("Domain Definition")]
        public AuthenticationTypes AuthenticationType
        {
            get { return atAuthType; }
            set
            {
                atAuthType = value;
                SetSearchRoot();
            }
        }

        /// <summary>
        /// Gets or sets the user name to use when authenticating the client.
        /// </summary>
        [Description("Account in domain to login to")]
        [Category("Logging Settings")]
        public string Account
        {
            get { return strAccount; }
            set
            {
                strAccount = value;
                SetSearchRoot();
            }
        }

        /// <summary>
        /// Gets or sets the password to use when authenticating the client.
        /// </summary>
        [DefaultValue("")]
        [Description("Password to login to account in domain")]
        [Category("Logging Settings")]
        public string Password
        {
            get { return strPassword; }
            set
            {
                strPassword = value;
                SetSearchRoot();
            }
        }

        /// <summary>
        /// Gets or sets nested levels to explore quantity.
        /// Set it to -1 to explore all available.
        /// </summary>
        [DefaultValue(-1)]
        [Description("Nested levels to explore quantity. " + 
                  "Set it to -1 to explore all available.")]
        [Category("Explore Settings")]
        public int NestedLevelsToExploreQuantity
        {
            get { return intNestedLevelCount; }
            set { intNestedLevelCount = value; }
        }

        private TreeNode GetChildrenOfDirectoryEntry(DirectoryEntry cde, 
                                                       int currentLevel)
        {
            try
            {
                if((intNestedLevelCount > 0)&&(currentLevel >= intNestedLevelCount))
                  return new TreeNode("Children: max. nested level reached", 7, 8);
                TreeNode tnChilds = new TreeNode("Children", 6, 8);
                foreach(DirectoryEntry de in cde.Children)
                {
                    tnChilds.Nodes.Add(GetNodeFromDirectoryEntry(de, currentLevel+1));
                }
                return tnChilds;
            }
            catch(Exception x)
            {
                return new TreeNode("<ERROR> "+x.Message, 7, 7);
            }
        }

        private TreeNode GetDescriptionOfDirectoryEntry(DirectoryEntry de)
        {
            try
            {
                TreeNode tnDescription = new TreeNode("Description", 9, 11);
                if(de.NativeGuid != null)
                  tnDescription.Nodes.Add(new TreeNode("Native GUID: "+
                                                de.NativeGuid, 12, 14));
                else
                  tnDescription.Nodes.Add(new 
                     TreeNode("Native GUID: <NULL>", 13, 14));
                tnDescription.Nodes.Add(new TreeNode("GUID: "+ 
                                 de.Guid.ToString(), 12, 14));
                tnDescription.Nodes.Add(new TreeNode("Authentication Type: " 
                                +de.AuthenticationType.ToString(), 12, 14));
                if(de.Password != null) tnDescription.Nodes.Add(new 
                       TreeNode("Password: "+de.Password, 12, 14));
                else
                  tnDescription.Nodes.Add(new 
                      TreeNode("Password: <UNAVAILABLE>", 13, 14));
                if(de.Path != null)
                  tnDescription.Nodes.Add(new TreeNode("Path: " 
                                            +de.Path, 12, 14));
                else 
                  tnDescription.Nodes.Add(new 
                     TreeNode("Path: <NULL>", 13, 14));
                if(de.SchemaClassName != null)
                  tnDescription.Nodes.Add(new TreeNode("Schema Class Name: " 
                                              +de.SchemaClassName, 12, 14));
                else 
                  tnDescription.Nodes.Add(new 
                      TreeNode("Schema Class Name: <NULL>", 13, 14));
                if(de.Username != null)
                  tnDescription.Nodes.Add(new TreeNode("User Name: "+ 
                                               de.Username, 12, 14));
                else
                  tnDescription.Nodes.Add(new 
                           TreeNode("User Name: <NULL>", 13, 14));
                if(de.Site != null)
                  tnDescription.Nodes.Add(new TreeNode("Site: " + 
                                          de.Site.Name, 12, 14));
                else
                  tnDescription.Nodes.Add(new 
                      TreeNode("Site: <NULL>", 13, 14));
                return tnDescription;
            }
            catch(Exception x)
            {
                return new TreeNode("<ERROR> "+x.Message, 13, 13);
            }
        }

        private TreeNode GetPropertiesFromDirectoryEntry(DirectoryEntry de)
        {
            try
            {
                TreeNode tnProperties = new TreeNode("Properties", 3, 5);
                foreach(string strPropertyName in de.Properties.PropertyNames)
                {
                    TreeNode tnCurrentProperty;
                    try
                    {
                        tnCurrentProperty = new TreeNode(strPropertyName, 15, 17);
                        foreach(object objValue in de.Properties[strPropertyName])
                            tnCurrentProperty.Nodes.Add(new 
                                TreeNode(objValue.ToString(), 12, 14));
                    }
                    catch(Exception x)
                    {
                        tnCurrentProperty = new TreeNode(strPropertyName+ 
                                     ": <ERROR>: "+x.Message, 16, 16);
                    }
                    tnProperties.Nodes.Add(tnCurrentProperty);
                }
                return tnProperties;
            }
            catch(Exception x)
            {
                return new TreeNode("<ERROR> "+x.Message, 4, 4);
            }
        }

        private TreeNode GetNodeFromDirectoryEntry(DirectoryEntry de, int currentLevel)
        {
            try
            {
                string strName = (de.Name == null) ? "<UNNAMED>" : de.Name;
                RaizeSearchInfoEvent("Creating "+strName+" entry...");
                TreeNode tnResult = new TreeNode(strName, 0, 2);
                tnResult.Nodes.Add(GetDescriptionOfDirectoryEntry(de));
                tnResult.Nodes.Add(GetPropertiesFromDirectoryEntry(de));
                tnResult.Nodes.Add(GetChildrenOfDirectoryEntry(de, currentLevel));
                RaizeSearchInfoEvent(strName+" added.");
                return tnResult;
            }
            catch(Exception x)
            {
                return new TreeNode("<ERROR> "+x.Message, 1, 1);
            }
        }

        /// <summary>
        /// Performs the search.
        /// </summary>
        /// <returns>Search results as tree nodes.</returns>
        public TreeNode[] Search()
        {
            RaizeSearchInfoEvent("Initializing...");
            ArrayList alResults = new ArrayList();
            SetSearchRoot();
            RaizeSearchInfoEvent("Starting search...");
            SearchResultCollection src = FindAll();
            RaizeSearchInfoEvent("Creating search results...");
            foreach(SearchResult sr in src)
              alResults.Add(GetNodeFromDirectoryEntry(
                            sr.GetDirectoryEntry(), 0));
            RaizeSearchInfoEvent("Search complete.");
            return (TreeNode[])(alResults.ToArray(typeof(TreeNode)));
        }

        /// <summary>
        /// The search information event handler.
        /// </summary>
        public delegate void SearchInfoEventHandler(string info);

        /// <summary>
        /// The search information ready event.
        /// </summary>
        public event SearchInfoEventHandler SearchInfo;

        /// <summary>
        /// Raises the event with given information.
        /// </summary>
        /// <param name="info">Information for the event.</param>
        private void RaizeSearchInfoEvent(string info)
        {
            if(SearchInfo != null) SearchInfo(info);
        }
    }
}

历史

  • 2005年6月17日:初始发布
© . All rights reserved.