使用 LDAP 过滤器查找所有结果。获取错误消息

发布于 2025-01-06 12:07:00 字数 626 浏览 2 评论 0原文

DirectoryEntry testAD = new DirectoryEntry();    
DirectorySearcher search = new DirectorySearcher(testAD);

StringBuilder add = new StringBuilder();
search.PropertiesToLoad.Add("mail");
search.Filter = "(&(objectClass=user))";

foreach (SearchResult SearchAll in search.FindAll())
{
    DirectoryEntry de = SearchAll.GetDirectoryEntry();
    add.Append(de.Properties["mail"].Value.ToString()); // error message here
}

PrefixDescription.Text = add.ToString();

我尝试首先查找所有电子邮件作为测试,然后查找所有信息(名字、姓氏等)并使用 LPAR 过滤器将其列出在文本框中,但在运行应用程序时不断收到此错误消息:

未将对象引用设置为对象的实例。

DirectoryEntry testAD = new DirectoryEntry();    
DirectorySearcher search = new DirectorySearcher(testAD);

StringBuilder add = new StringBuilder();
search.PropertiesToLoad.Add("mail");
search.Filter = "(&(objectClass=user))";

foreach (SearchResult SearchAll in search.FindAll())
{
    DirectoryEntry de = SearchAll.GetDirectoryEntry();
    add.Append(de.Properties["mail"].Value.ToString()); // error message here
}

PrefixDescription.Text = add.ToString();

I'm trying to find all emails first as a test and then all information (first name, last name, etc) and list it in a text box using a LPAR filter but I keep getting this error message when I run the app:

Object reference not set to an instance of an object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜柠檬 2025-01-13 12:07:00

好吧,您正在枚举用户 - 但您不能保证最终的用户将拥有电子邮件地址!您需要基本的“编程 101”错误预防:

.....
foreach (SearchResult result in search.FindAll())
{
    // this is totally unnecessary - the "SearchResult" already *contains* all
    // the properties you've defined in your "PropertiesToLoad" collection!
    // DirectoryEntry de = SearchAll.GetDirectoryEntry();

    if(result.Properties["mail"] != null && result.Properties["mail"].Count > 0)
    {
        add.Append(result.Properties["mail"][0].ToString());
    }
}

通过此额外检查,您可以避免 Object reference not set.... 错误...

Well, you're enumerating users - but you have no guarantee that the resulting user will have an e-mail address! You need basic "programming 101" error prevention:

.....
foreach (SearchResult result in search.FindAll())
{
    // this is totally unnecessary - the "SearchResult" already *contains* all
    // the properties you've defined in your "PropertiesToLoad" collection!
    // DirectoryEntry de = SearchAll.GetDirectoryEntry();

    if(result.Properties["mail"] != null && result.Properties["mail"].Count > 0)
    {
        add.Append(result.Properties["mail"][0].ToString());
    }
}

With this extra check, you avoid the Object reference not set.... error...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文