Click here to Skip to main content
15,118,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I´m trying to connect to an local LDAP server which i have set up using Apache Directory Studio. I´m using Visual Studio 2015 enterprise update 2 and MVC 5.

What I have tried:

These are the two approaches I´m working with:

C#
public void ldap()
    {

        try
        {
            DirectoryEntry entry = new DirectoryEntry("LDAP://localhost:10389");
            DirectorySearcher mySearcher = new DirectorySearcher(entry);

            var result = mySearcher.FindOne();

        }
        catch (System.Runtime.InteropServices.COMException)
        {
            System.Runtime.InteropServices.COMException exception = new System.Runtime.InteropServices.COMException();
            Console.WriteLine(exception);
        }
        catch (InvalidOperationException)
        {
            InvalidOperationException InvOpEx = new InvalidOperationException();
            Console.WriteLine(InvOpEx.Message);
        }
        catch (NotSupportedException)
        {
            NotSupportedException NotSuppEx = new NotSupportedException();
            Console.WriteLine(NotSuppEx.Message);
        }
     }

After var result = mySearcher.FindOne() is executed, an System.Runtime.InteropServices.COMException' in System.DirectoryServices.dll occures, stating that an HRESULT E_FAIL-error was passed when calling a COM-Component.

I have no idea what that means and didn´t find something helpful using Google.

2nd approach:

C#
try
        {
            LdapConnection ldapConnection = new LdapConnection("LDAP://localhost:10389");

            var networkCredential = new NetworkCredential("cbrunato", "c2VjcmV0", "dc=example,dc=com");
            ldapConnection.SessionOptions.SecureSocketLayer = true;
            ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
            ldapConnection.AuthType = AuthType.Negotiate;
            ldapConnection.Bind(networkCredential);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

After ldapConnection.Bind(networkCredential) is executed, i get the error message "LDAP server is not available".

When i stop the ldap Server and run the program i get the same errors, so i guess i don´t get a connection to my ldap server with both approaches, but i have no clue why.

I´m very thankful for any help.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900