获取Active Directory中的用户列表

时间:2012-11-21 15:36:07

标签: c# active-directory

我正在编写一个需要从指定域获取用户列表的应用程序。我现在可以得到用户,但是这样做可以减慢我如何更快地完成这项工作,尤其是在较大的域名上?

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain))
        {
            UserPrincipal userPrincipal = new UserPrincipal(pc);

            PrincipalSearcher search = new PrincipalSearcher(userPrincipal);

            PrincipalSearchResult<Principal> results = search.FindAll();

            foreach (var principals in results.Partition(20))
            {
                IDictionary<string, string> users = new Dictionary<string, string>(20);
                foreach (UserPrincipal principal in principals.Cast<UserPrincipal>())
                {
                    users.Add(principal.SamAccountName, principal.DisplayName);
                }

                yield return users;
            }
        }

基本上在外部foreach循环中我得到IEnumerable IEnumerable&gt;。我做到了这一点,所以我可以尝试逐步加载一些并将其显示给用户,而其余的仍在加载,但是一旦我点击那个内循环,我就会挂几分钟。

我正在尝试以域\用户名格式获取用户名,我还没想出如何做。

3 个答案:

答案 0 :(得分:2)

试试这个。它应该更快。

using System.DirectoryServices;

string[] RetProps = new string[] { "SamAccountName", "DisplayName" };
                //IDictionary<string, string> users = new Dictionary<string, string>();
List<string[]> users = new List<string[]>();

           foreach (SearchResult User in GetAllUsers("YourDomain", RetProps))
           {
            DirectoryEntry DE = User.GetDirectoryEntry();
            try
               {
                users.Add(new string[]{DE.Properties["SamAccountName"][0].ToString(), DE.Properties["DisplayName"][0].ToString()});
               }
               catch
               {
               }
            }


    internal static SearchResultCollection GetAllUsers(string DomainName, string[] Properties)
    {
      DirectoryEntry DE = new DirectoryEntry("LDAP://" + DomainName);
      string Filter = "(&(objectCategory=organizationalPerson)(objectClass=User))";
      DirectorySearcher DS = new DirectorySearcher(DE);
      DS.PageSize = 10000;
      DS.SizeLimit = 10000;
      DS.SearchScope = SearchScope.Subtree;
      DS.PropertiesToLoad.AddRange(Properties); DS.Filter = Filter;
      SearchResultCollection RetObjects = DS.FindAll();
      return RetObjects;
    }
  }
}

答案 1 :(得分:0)

这很快。我在几秒钟内得到了14k的结果。如果需要更新ui,可以使用eventhandler和其他线程。

  string groupName = "Domain Users";
            string domainName = "domain";
            var results = new List<string>();
            using (var pc = new PrincipalContext(ContextType.Domain, domainName))
            {
                using (var grp = GroupPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, groupName))
                {
                    if (grp != null)
                        foreach (var p in grp.GetMembers(false))
                        {
                            results.Add(p.DisplayName);
                        }
                }
                Assert.IsTrue(results.Count > 0);
            }

答案 2 :(得分:0)

dc是域cntroller,例如“company.com”

public static ArrayList GetAllActiveDirectoryUsersByDisplayName(string dc) 
            {
                ArrayList list = new ArrayList();

                PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dc);
                UserPrincipal u = new UserPrincipal(ctx);

                PrincipalSearcher ps = new PrincipalSearcher(u);
                PrincipalSearchResult<Principal> results = ps.FindAll();

                foreach (UserPrincipal usr in results)
                {
                    list.Add(usr.Name);
                }

            list.Sort();

            return list; 
        }

在你的最后,你可以这样做:

ArrayList list = GetAllActiveDirectoryUsersByDisplayName("company.com");

                foreach (string x in list)
                {
                  Console.WriteLine(x);
                }