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

use*_*358 6 .net c# 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()
Run Code Online (Sandbox Code Playgroud)

这是C#错误:

System.Runtime.InteropServices.COMException was unhandled
Message="The parameter is incorrect.\r\n"
Source="System.DirectoryServices"
ErrorCode=-2147024809
Run Code Online (Sandbox Code Playgroud)

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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是有效的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
Run Code Online (Sandbox Code Playgroud)

Gra*_*ark 8

我猜想在VB.NET代码中,你是在构造函数中传入vbNull(代替Nothing)两个参数DirectoryEntry,而在传递的C#代码中传递null.vbNull大概来自Microsoft.VisualBasic不应该使用的邪恶集会.

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

查看VB.NET代码是否在您使用时抛出异常Nothing,或者通过使用String.Empty而不是查看C#代码是否有效null.

此外,在您的C#代码中,调用FindAll是在try块之外.


Joh*_*ers 1

C# 和 VB.NET 都不实现DirectorySearcher.NET 或任何其他部分。它们都是 .NET Framework 的一部分。