可以提高AD查询时间吗?

本文关键字:时间 查询 AD | 更新日期: 2023-09-27 18:03:41

根据这里找到的代码,我正在查询一个AD组以填充关联的下拉列表。下面的代码运行大约需要30秒,所以我只是想确保我在使用最好的代码?理想情况下,我希望它运行得更快,但估计1500人不会花几秒钟的时间。

            // define AD group to use for populating associate list...
        const string groupName = "Group-Name-X";
        // define array objects...
        var userIdLookup = new SortedList();
        var emailLookup = new SortedList();
        var listContents = new SortedList<string, string>();
        // initialise the AD tools class...
        var adm = new ADMethods();
        // get the members of the specified AD group...
        GroupPrincipal group = adm.GetGroup(groupName);
        // iterate over its members
        foreach (Principal p in group.Members)
        {
            // get the data for the user...
            var groupMember = adm.GetUser(p.SamAccountName);
            // if user account found...
            if (groupMember != null)
            {
                // get the user's mail address...
                string mailAddress = groupMember.EmailAddress ?? p.SamAccountName;
                // if domain extension present ...
                if (mailAddress.Contains("@"))
                {
                    // remove it...
                    mailAddress = mailAddress.Split('@')[0].Trim();
                }
                // create a name starting with surname...
                string associateName = string.Format("{0},", groupMember.Surname);
                // append the first name...
                associateName += string.Format(" {0}", groupMember.GivenName);
                // if the middle name is present...
                if (groupMember.MiddleName != string.Empty)
                {
                    // append it to the string...
                    associateName += string.Format(" {0}", groupMember.MiddleName);
                }
                // get the employee ID...
                string associateNumber = p.SamAccountName;
                // if associate already added...
                if (listContents.ContainsKey(associateName))
                {
                    // skip rest of code for this iteration...
                    continue;
                }
                // add to list that will populate dropdown...
                listContents.Add(associateName, mailAddress);
                // add to lookup collection of ccmail to userID
                userIdLookup.Add(mailAddress, associateNumber);
                // add associate name to list...
                emailLookup.Add(associateNumber, mailAddress);
            }
        }

可以提高AD查询时间吗?

您可以考虑对特定组的成员执行属性范围的查询。由于它目前是实现的,因此最可能的情况是您必须对每个成员执行新的搜索(您将需要使用类似Stopwatch的东西来验证)。使用属性范围查询,您可以一次加载所有用户,然后对他们进行工作。

我没有尝试过System.DirectoryServices。帐户管理,但我知道你可以用系统来做。

目录服务和系统。

DirectorySearcher示例