我有一个方法,我使用获取活动目录中的部门列表。然而,有些人(至少有一个我所知道的)没有出现。 “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;
}
答案 0 :(得分:2)
是否有超过1000名用户?如果是这样,您可能达到了限制described in the answer to this question。
尝试设置:
search.PageSize = ... some non-zero value ...;
此外,您应该使用使用声明处理您的一次性物品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 ( ))
{
...
}
}
}
}