【问题标题】:Unhandled Exception Active Directory未处理的异常 Active Directory
【发布时间】:2014-08-12 15:57:06
【问题描述】:

我是 Active Directory 的新手,我正在尝试显示 Active Directory 中组类别的详细信息。这是我的以下代码:

entry = new DirectoryEntry(strPath);

DirectoryEntry schema = entry.SchemaEntry;
System.DirectoryServices.DirectorySearcher myNewSearcher = new System.DirectoryServices.DirectorySearcher(entry);

mySearcher.Filter = ("(objectClass=*)");

foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
{
    Console.WriteLine("First Name: " + resEnt.Properties["givenName"][0].ToString());
    Console.WriteLine("Last Name : " + resEnt.Properties["sn"][0].ToString());
    Console.WriteLine("SAM account name   : " + resEnt.Properties["samAccountName"][0].ToString());
    Console.WriteLine("User principal name: " + resEnt.Properties["userPrincipalName"][0].ToString());
    Console.WriteLine();
}

我得到的错误是:

在 mscorlib.dll 中发生“System.ArgumentOutOfRangeException”类型的未处理异常附加信息:索引超出范围。必须为非负数且小于集合的大小。

我能得到一些帮助吗?

【问题讨论】:

    标签: c# .net active-directory


    【解决方案1】:

    可能未设置 AD 属性 - 在这种情况下,您调用 .Properties[..][0] 将导致此异常。

    您需要检查从 DirectorySearcher 返回的每个属性是否已设置:

    if(resEnt.Properties["givenName"] != null && resEnt.Properties["givenName"].Count > 0) {
       Console.WriteLine("First Name: " + resEnt.Properties["givenName"][0].ToString());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      相关资源
      最近更新 更多