因为对AD操作不是很熟悉,所以想请问哪位高手知道如何去遍历指定的AD域中所有用户并校验其密码是否为123456,希望能给出代码

解决方案 »

  1.   

    用自带的DirectoryEntry组件即可
      

  2.   

    在网上找到的,拿出来分享下
    public void AuthenticateGetAll()
            {
                //DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainName + "/" + ouName, adAdmin, password, AuthenticationTypes.Secure);
                //System.DirectoryServices.DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + domainName, adAdmin, password, AuthenticationTypes.Secure);
                DirectoryEntry ou = entry.Children.Find("OU=" + ouName);            DirectorySearcher mySearcher = new DirectorySearcher(ou);//想搜索出所有,此处可省参数            mySearcher.Filter = "(|(objectClass=user)(objectClass=organizationalUnit))";
                mySearcher.PageSize = 20000;
                foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
                {
                    System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();                
                    // 通过objectcategory属性判断节点是组织单元还是用户,并设置不同的Treeview节点显示图片
                    if (de.Properties["objectcategory"].Value.ToString().Split(',')[0].ToString() != "CN=Organizational-Unit")                
                    {
                        if (TryAuthenticate(domainName, de.Properties["sAMAccountName"].Value.ToString(), "123456"))
                        {
                            //textBox1.Text += de.Properties["sAMAccountName"].Value.ToString() + " " + de.Properties["name"].Value.ToString() + " \r\n";
                            
                           
                        }
                    }
                    
                }
            }        /// <summary>
            /// 验证AD用户是否登录成功
            /// </summary>
            /// <param name="domain"></param>
            /// <param name="userName"></param>
            /// <param name="password"></param>
            /// <returns></returns>
            public bool TryAuthenticate(string domain, string userName, string password)
            {
                bool isLogin = false;
                try
                {
                    DirectoryEntry entry = new DirectoryEntry(string.Format("LDAP://{0}", domain), userName, password);
                    entry.RefreshCache();
                    isLogin = true;
                }
                catch
                {
                    isLogin = false;
                }
                return isLogin;
            }