C # and VB.NET LDAP Search Different?

Does anyone know if there is a difference between the implementation of the FindAll () method in a DirectorySearcher object in C # and VB.NET? In my opinion, they are both β€œcompiled” in MSIL and handled by the CLR the same way. Against our ADAM / LDAP system, the C # code below causes an error, and below VB.NET does not.

Here are the C # exception stacks:

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:

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

C # code:

 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:

 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 
+6
source share
2 answers

I am going to assume that in VB.NET code you pass vbNull (instead of Nothing ) for two parameters in the DirectoryEntry constructor and in C # code that you pass null . vbNull supposedly from the Microsoft.VisualBasic evil build, which should not be used.

The constructor for DirectoryEntry checks the username and password parameters to see if they are null. If vbNull != Nothing , the constructor will not treat them as null and will behave differently.

See if VB.NET code throws an exception if you use Nothing or, conversely, see if C # code works using String.Empty instead of null .

Also, in C # code, the call to FindAll is outside the try block.

+8
source

Neither C # nor VB.NET implement DirectorySearcher or any other part of .NET. All of them are part of the .NET Framework.

+1
source

Source: https://habr.com/ru/post/888651/


All Articles