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:
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:
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.