0

我有一个方法,我正在使用获取我们活动目录中的部门列表。但是有些(至少我知道的)没有出现。“EMP-Alumni Relations”是我目前正在解决的问题。

这是我正在使用的代码。如果有人能找出任何潜在的问题,我将不胜感激。我一时不知所措。我已经确定了部门中的几个用户,所以我知道这不应该是问题。

ArrayList GetAdDepts (  )
    {
        DirectoryEntry myLdapConnection = SCDirectoryEntry.GetDirectoryEntry ( );
        DirectorySearcher search = new DirectorySearcher ( myLdapConnection );
        search.Filter = "(objectClass=user)";
        search.PropertiesToLoad.Add ( "department" );
        SearchResultCollection result = search.FindAll ( );
        ArrayList departments = new ArrayList ( );
        foreach ( SearchResult depart in result )
        {
            DirectoryEntry directoryEntry = depart.GetDirectoryEntry ( );

            if ( directoryEntry.Properties.Contains ( "department" ) )
            {
                string dept = ( string ) depart.Properties [ "department" ] [ 0 ];
                if ( dept.Trim ( ).StartsWith ( "EMP-" ) )
                {
                    if ( !departments.Contains ( dept ) )
                    {
                        departments.Add ( dept );
                    }
                }
            }

        }
        return departments;
    }
4

1 回答 1

2

有超过 1000 个用户吗?如果是这样,您可能已达到此问题的答案中描述的限制。

尝试设置:

search.PageSize = ... some non-zero value ...;

此外,您应该使用 using 语句处理一次性物品DirectorySearcher, SearchResultCollection, ,例如:DirectoryEntry

using (var search = new DirectorySearcher(myLdapConnection ))
{
    search.Filter = "(objectClass=user)";          
    search.PropertiesToLoad.Add ( "department" );          
    search.PageSize = 1000; // any non-zero value will work
    using (var result = search.FindAll ( ))
    {
        ...
        foreach ( SearchResult depart in result )             
        {                 
            using (var directoryEntry = depart.GetDirectoryEntry ( ))
            {
                ...
            }
        }
    }
}
于 2012-09-25T13:58:02.443 回答