LDAP and SSL with C#

A

Andrew

How do I make an LDAP query using the DirectoryEntry or
other C# classes that goes through SSL?

Thanks,
 
M

Marc Scheuner [MVP ADSI]

How do I make an LDAP query using the DirectoryEntry or
other C# classes that goes through SSL?

Use the constructor for DirectoryEntry which allows you to specify the
"AuthenticationTypes":

DirectoryEntry deUser = new DirectoryEntry(<LDAP path>, <user name>,
<password>, AuthenticationTypes.SecureSocketsLayer));


Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
J

Jonathan Eric Miller

Although, to my knowledge it isn't documented, I found that it works by
simply setting the port to the default LDAPS port which is 636. Not
suprisingly, Microsoft's documentation doesn't even describe what the URL
syntax is for using DirectoryEntry with non-Active Directory LDAP servers.

using System;
using System.DirectoryServices;
namespace LdapSearch {
public class LdapSearch {
public static void Main(string [] args) {
try {
if(args.Length != 6) {
Console.WriteLine("LdapSearch host port user password baseDN filter");
return;
}
string host = args[0];
string port = args[1];
string user = args[2];
string password = args[3];
string baseDN = args[4];
string filter = args[5];
DirectoryEntry de = new DirectoryEntry("LDAP://" + host + ":" + port + "/" +
baseDN);
de.Username = user;
de.Password = password;
de.AuthenticationType = AuthenticationTypes.FastBind;
DirectorySearcher ds = new DirectorySearcher(de, filter);
SearchResultCollection src = ds.FindAll();
foreach(SearchResult sr in src) {
Console.WriteLine(sr.Path);
foreach(string s in sr.Properties.PropertyNames) {
Console.Write(s + ":");
foreach(object o in sr.Properties) {
Console.Write(" " + o);
}
Console.WriteLine();
}
}
}
catch(Exception e) {
Console.WriteLine(e);
}
}
}
}

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top