0

我正在尝试从活动目录中获取所有组及其子组和每个组的成员,然后将其子组的每个组链接到树结构中,然后将结果存储在数据库中,这样我就可以找出什么是父级和子级每组。

如何转换以下代码DirectoryServices来测试性能?

public static List<Group>getUsers()
    {

        // create the "context" in which to operate - your domain here, 
        // as the old-style NetBIOS domain, and the container where to operate in
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "lin.proximus.com");

        // define a "prototype" - an example of what you're searching for
        // Here: just a simple GroupPrincipal - you want all groups
        GroupPrincipal prototype = new GroupPrincipal(ctx);

        // define a PrincipalSearcher to find those principals that match your prototype
        PrincipalSearcher searcher = new PrincipalSearcher(prototype);

        // define a list of strings to hold the group names        
        List<Group> groupNames = new List<Group>();
        int counter = 0;
        // iterate over the result of the .FindAll() call
        foreach (var gp in searcher.FindAll())
        {

            // cast result to GroupPrincipal
            GroupPrincipal groupPrincipal = gp as GroupPrincipal;

            // if everything - grab the group's name and put it into the list
            if (groupPrincipal == null) continue;

            Group group = new Group();
            group.Name = groupPrincipal.Name;
            group.Description = groupPrincipal.Description;
            AddSubGroups(groupPrincipal, ref group);
            AddMemebers(groupPrincipal, ref group);
            counter++;
            groupNames.Add(group);
            Console.WriteLine(counter);
            if (counter > 10000)
                return groupNames;
        }
        return groupNames;
    }

    private static void AddSubGroups(GroupPrincipal gp,ref Group gr)
    {
        gr.SubCounts = 0;
        if (gp.GetGroups().Count() <= 0) return;

        gr.SubCounts = gp.GetGroups().Count();
        gr.SubGroups = new List<string>();
        foreach (var principal in gp.GetGroups())
        {
            gr.SubGroups.Add(principal.Name);
        }
    }

    private static void AddMemebers(GroupPrincipal gp, ref Group gr)
    {
        if (gp.GetMembers().Count() <= 0) return;

        gr.Users = new List<string>();

        foreach (Principal principal in gp.GetMembers())
        {
            gr.Users.Add(principal.Name);
        }
    }
4

1 回答 1

0

我赞同 Marc 的建议 - 使用分页和适当的 pagesize 属性值来提高性能。AFAIK,如果您想获得可能具有> 999 个值的属性(例如组的成员资格),分页实际上是必不可少的。

于 2013-01-30T07:29:11.710 回答