1

In Active directory, I am trying to connect to LDAP server and trying to check if user us valid or not. But the below code throws the exception error. I am not sure where i am going wrong.. Even whether the connection made is correct or not. I tried the same connection name and password into the Apache directory studio which i am able to connect successfully but not through the code.It throws the exception from myLdapConnection please can you check where i am going wrong. My connection which i am using in the Apache directory studio is secure connection .. Do i need to specify the LDAPS or ldap will work

public void GetAllADUsers( string username)
{
    try
    {

        DirectoryEntry myLdapConnection = new DirectoryEntry();
myLdapConnection.Path = "LDAP://server:port/o=value";
myLdapConnection.Username = @"CN=cn,OU=ou,OU=Users,O=o";
myLdapConnection.Password = "password";

//myLdapConnection.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ( "(&(objectClass=user)") };

     SearchResultCollection allResults = search.FindAll();
        if(allResults.Count == 0)
{

            Console.WriteLine("not found");
 }
else
{
Console.WriteLine ("found");
}
catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
 }

Note username if i specify = @"CN=cn,OU=ou,OU=Users,O=o"; myLdapConnection threw the DirectorySearcherCOMException but if only pass the username =cn means on cn value.. this gives all the values

exception

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)\r\n at System.DirectoryServices.DirectoryEntry.Bind()\r\n at System.DirectoryServices.DirectoryEntry.get_AdsObject()\r\n at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)\r\n at System.DirectoryServices.DirectorySearcher.FindAll()\r\n at _Default.GetAllADUsers(String username)

user3226440
  • 559
  • 1
  • 8
  • 23
  • can you try the username as `joe.bloggs@company.com`? Also, what is the exception (apart from ComException)? Also, `(&(objectClass=user)` is incorrect LDAP syntax, try `(objectClass=user)`. – zaitsman Aug 13 '14 at 09:39
  • @zaitsman done the same but same except which is /o=textron.com `Stepping over non-user code 'System.DirectoryServices.DirectorySearcher.FindAll' The thread '' (0x22a0) has exited with code 0 (0x0). The thread '' (0x20ac) has exited with code 0 (0x0).` – user3226440 Aug 13 '14 at 10:05
  • what you posted is NOT an exception. Also, what username did you specify? – zaitsman Aug 13 '14 at 10:09
  • @zaitsman please check the post above ... I have edited the exception and username if trying with above and only `cn` value both are not working – user3226440 Aug 13 '14 at 10:16
  • you posted PART of your stack trace; as for the CN, can you please try with user@company.com WITHOUT ANY CN – zaitsman Aug 13 '14 at 10:54

0 Answers0