简体   繁体   English

从Active Directory获取部门列表缺少某些部门

[英]Get List of Departments from Active Directory is missing certain departments

I have a method that I am using the get a list of departments in our active directory. 我有一个方法,我正在使用我们的活动目录中的部门列表。 However some (at least one that I know of) is not showing up. 然而,有些人(至少有一个我所知道的)没有出现。 "EMP-Alumni Relations" is the one I am troubleshooting at the moment. “EMP-Alumni Relations”是我目前正在排除的问题。

Here is the code I am using. 这是我正在使用的代码。 If anyone can identify any potential problems I would appreciate it. 如果有人能够发现任何潜在的问题,我将不胜感激。 I am at a loss for the moment. 我暂时不知所措。 I have identified several users who are in the department so I know that should not be the issue. 我已经确定了几个部门的用户,所以我知道不应该是问题。

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;
    }

Are there more than 1000 users? 有超过1000个用户吗? If so you are probably hitting the limit described in the answer to this question . 如果是这样,你可能达到了这个问题的答案中描述的限制。

Try setting: 尝试设置:

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

Also you should be disposing your disposable objects DirectorySearcher , SearchResultCollection , DirectoryEntry , with a using statement eg: 您还应该使用using语句处理一次性对象DirectorySearcherSearchResultCollectionDirectoryEntry ,例如:

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 ( ))
            {
                ...
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM