How to connect to an local ldap server using MVC 5
<p>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.</p>

<p>These are the two approaches I´m working with:</p>

<pre><code> 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);
        }
     }
</code></pre>

<p>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.</p>

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

<p>2nd approach:</p>

<pre><code>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);
        }
</code></pre>

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

<p>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.</p>

<p>I´m very thankful for any help.</p>

<p>Update: Credentials are invalid exception</p>

<p><a href="http://i.stack.imgur.com/5DJtg.png" rel="nofollow">Apache Directory Studio</a>    </p>

<p>Strings used as username:</p>

<pre><code>"uid=cbrunato"
"cn=Chuck Brunato"
"cn=Chuck Brunato,ou=Users,dc=example,dc=com"
"uid=cbrunato,cn=Chuck Brunato,ou=Users,dc=example,dc=com"
"uid=cbrunato,ou=Users,dc=example,dc=com"
</code></pre>