【发布时间】:2012-09-05 08:37:47
【问题描述】:
所以我现在在一个特定的 AD 组中有超过 1500 个用户,当我将他们拉下来时,我会限制我得到的用户。我在 MSDN (http://msdn.microsoft.com/en-us/library/ms180907%28v=vs.80%29.aspx) 上看到了这篇文章,但是它执行FindOne(),这样做会使应用程序超过 10分钟拉下用户。使用ResultsCollection,我可以在 30 秒内打开应用程序。
什么时候去处理
string Last_Name = userResults.Properties["sn"][0].ToString();
返回错误:
索引超出范围。必须是非负数且小于 集合。\r\n参数名称:索引"}
我认为这存在找不到结果的问题,但是,ResultsCollection 包含所有 1000 个条目。任何帮助表示赞赏。谢谢!
注意:这些用户的姓氏不能为空,问题是resultCollection 只返回 1 个属性,即adpath
DirectoryEntry dEntryhighlevel = new DirectoryEntry("LDAP://OU=Clients,OU=x,DC=h,DC=nt");
DirectorySearcher dSeacher = new DirectorySearcher(dEntryhighlevel);
dSeacher.Filter = "(&(objectClass=user)(memberof=CN=Users,,OU=Clients,OU=x,DC=h,DC=nt))";
uint rangeStep = 1000;
uint rangeLow = 1;
uint rangeHigh = rangeLow + (rangeStep -1);
bool lastQuery = false;
bool quitLoop = false;
do
{
string attributeWithRange;
if (!lastQuery)
{
attributeWithRange = String.Format("member;range={0}-{1}", rangeLow, rangeHigh);
}
else
{
attributeWithRange = String.Format("member;range={0}-*", rangeLow);
}
dSeacher.PropertiesToLoad.Clear();
dSeacher.PropertiesToLoad.Add(attributeWithRange);
SearchResultCollection resultCollection = dSeacher.FindAll();
foreach (SearchResult userResults in resultCollection)
{
string Last_Name = userResults.Properties["sn"][0].ToString();
string First_Name = userResults.Properties["givenname"][0].ToString();
string userName = userResults.Properties["samAccountName"][0].ToString();
string Email_Address = userResults.Properties["mail"][0].ToString();
OriginalList.Add(Last_Name + "|" + First_Name + "|" + userName + "|" + Email_Address);
if (userResults.Properties.Contains(attributeWithRange))
{
foreach (object obj in userResults.Properties[attributeWithRange])
{
Console.WriteLine(obj.GetType());
if (obj.GetType().Equals(typeof(System.String)))
{
}
else if (obj.GetType().Equals(typeof(System.Int32)))
{
}
Console.WriteLine(obj.ToString());
}
if (lastQuery)
{
quitLoop = true;
}
}
else
{
lastQuery = true;
}
if (!lastQuery)
{
rangeLow = rangeHigh + 1;
rangeHigh = rangeLow + (rangeStep - 1);
}
}
}
while (!quitLoop);
【问题讨论】:
-
如果姓氏为空,很可能返回
null。这就是为什么你不能要求它的第一个字母 (["sn"][0])。这会给你空引用。 -
@WiktorZychla 这些用户的姓氏不为空,问题是 resultCollection 只返回 1 个属性,那就是“adpath”
标签: c# active-directory