简体   繁体   English

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

[英]C# and VB.NET LDAP Search Different?

Does anyone know if there is a difference between the implementation of the FindAll() method on the DirectorySearcher object in C# and VB.NET? 有谁知道C#和VB.NET中DirectorySearcher对象上的FindAll()方法的实现是否有区别? From my understanding they both get "compiled" to MSIL and get processed by the CLR the same way. 根据我的理解,它们都被“编译”到MSIL并由CLR以相同的方式处理。 Going against our ADAM/LDAP system the below C# code throws an error and the below VB.NET does not. 与我们的ADAM / LDAP系统相反,下面的C#代码会抛出错误,而下面的VB.NET则不会。

Here is the C# exception stack: 这是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()

Here is the C# error: 这是C#错误:

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

C# code: 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.");
    }
}

This is the VB.NET code that works: 这是有效的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

I am going to guess that in the VB.NET code, you're passing in vbNull (instead of Nothing ) for two parameters in the DirectoryEntry constructor, and in the C# code you're passing null . 我猜想在VB.NET代码中,你在DirectoryEntry构造函数中为两个参数传递vbNull (而不是Nothing ),并且在C#代码中传递null vbNull is presumably from the evil Microsoft.VisualBasic assembly which should not be used. vbNull大概来自邪恶的Microsoft.VisualBasic程序集,不应该使用它。

The constructor for DirectoryEntry checks the username and password parameters to see if they're null. DirectoryEntry的构造函数检查用户名和密码参数以查看它们是否为空。 If vbNull != Nothing , the constructor won't treat them as null and will behave differently. 如果vbNull != Nothing ,构造函数将不会将它们视为null并且行为会有所不同。

See if the VB.NET code throws the exception if you use Nothing , or alternatively see if the C# code works by using String.Empty instead of null . 如果你使用Nothing ,看看VB.NET代码是否抛出异常,或者看看C#代码是否通过使用String.Empty而不是null

Also, in your C# code, the call to FindAll is outside of the try block. 此外,在您的C#代码中,对FindAll的调用不在try块之内。

Neither C# nor VB.NET implement DirectorySearcher or any other part of .NET. C#和VB.NET都没有实现DirectorySearcher或.NET的任何其他部分。 They are all part of the .NET Framework. 它们都是.NET Framework的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM