1

我正在编写一个需要从指定域获取用户列表的应用程序。我现在可以获得用户,但这是一种减慢速度的方式,尤其是在较大的域上,我怎样才能更快地做到这一点?

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。我这样做是为了尝试一次递增地加载一些并将它们显示给用户,而其余的仍在加载,但是一旦我点击了那个内部循环,我就会挂起几分钟。

我也在尝试以域\用户名格式获取用户名,但我还没有弄清楚该怎么做。

4

3 回答 3

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;
    }
  }
}
于 2012-11-23T10:24:51.090 回答
0

这是相当快的。我在几秒钟内得到了 14k 个结果。如果您需要更新 ui,您可以使用事件处理程序和不同的线程。

  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);
            }
于 2012-11-21T15:51:38.157 回答
0

dc 是域控制器,例如“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);
                }
于 2013-01-11T09:34:44.627 回答