C# 和 VB.NET LDAP 搜索不同?

标签 c# .net vb.net exception-handling ldap

有谁知道在 C# 和 VB.NET 中对 DirectorySearcher 对象的 FindAll() 方法的实现是否有区别?据我了解,它们都被“编译”为 MSIL,并以相同的方式被 CLR 处理。与我们的 ADAM/LDAP 系统相反,下面的 C# 代码会抛出错误,而下面的 VB.NET 则不会。

这是 C# 异常堆栈:

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
  at System.DirectoryServices.DirectoryEntry.Bind()
  at System.DirectoryServices.DirectoryEntry.get_AdsObject()
  at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
  at System.DirectoryServices.DirectorySearcher.FindAll()

这是 C# 错误:

System.Runtime.InteropServices.COMException was unhandled
Message="The parameter is incorrect.\r\n"
Source="System.DirectoryServices"
ErrorCode=-2147024809

C#代码:

private void button1_Click(object sender, EventArgs e)
{
    DirectoryEntry root = new DirectoryEntry("LDAP://directory.corp.com/OU=Person,OU=Lookups,O=Corp,C=US", null, null, AuthenticationTypes.Anonymous);
    DirectorySearcher mySearcher = new DirectorySearcher(root);

    mySearcher.Filter = "(uid=ssnlxxx)";
    mySearcher.PropertiesToLoad.Add("cn");
    mySearcher.PropertiesToLoad.Add("mail");

    SearchResultCollection searchResultCollection = null;
    searchResultCollection = mySearcher.FindAll(); //this is where the error occurs

    try
    {
        foreach (SearchResult resEnt in searchResultCollection)
        {
            Console.Write(resEnt.Properties["cn"][0].ToString());
            Console.Write(resEnt.Properties["mail"][0].ToString());
        }
    }
    catch (DirectoryServicesCOMException ex)
    {
        MessageBox.Show("Failed to connect LDAP domain, Check username or password to get user details.");
    }
}

这是有效的 VB.NET 代码:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

    Dim root As New     DirectoryEntry("LDAP://directory.corp.com/OU=People,OU=Lookups,O=corp,C=US", vbNull, vbNull, authenticationType:=DirectoryServices.AuthenticationTypes.Anonymous)
    Dim searcher As New DirectorySearcher(root)
    searcher.Filter = "(uid=ssnlxxx)"
    searcher.PropertiesToLoad.Add("cn")
    searcher.PropertiesToLoad.Add("mail")

    Dim results As SearchResultCollection

    Try
        results = searcher.FindAll()

        Dim result As SearchResult
        For Each result In results
            Console.WriteLine(result.Properties("cn")(0))
            Console.WriteLine(result.Properties("mail")(0))
        Next result
    Catch ex As Exception
        MessageBox.Show("There was an error")
    End Try
End Sub

最佳答案

我猜想在 VB.NET 代码中,您要为 DirectoryEntry 中的两个参数传入 vbNull(而不是 Nothing) 构造函数,并在 C# 代码中传递 nullvbNull 可能来自不应使用的邪恶 Microsoft.VisualBasic 程序集。

DirectoryEntry 的构造函数检查用户名和密码参数以查看它们是否为空。如果 vbNull != Nothing,构造函数不会将它们视为 null 并且行为会有所不同。

如果您使用 Nothing,请查看 VB.NET 代码是否抛出异常,或者通过使用 String.Empty 而不是 查看 C# 代码是否工作空

此外,在您的 C# 代码中,对 FindAll 的调用在 try block 之外。

关于C# 和 VB.NET LDAP 搜索不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6073718/

相关文章:

c# - 如何使用 SqlDataReader 获取列的数据类型和大小?

c# - 在动态加载的第三方程序集中使用静态类

c# - 在单独的窗体上刷新对象

vb.net - 您的许可证在差异工具中无效

c# - 在 C# 中从服务器向客户端发送一些约束

c# - 无法在 Visual Studio 2013 中创建新的 Web 项目

c# - 打开已经打开的文件时出现异常

c# - 是否需要实例化一个类以便您读取其属性之一?

sql - 如何在 VB.NET 上执行存储过程

c# - 检查用户是否登录