C#: How to Connect to Active Directory with SSL Enabled using DirectoryContext, Domains and DirectoryEntry classes?

I want to search all the Domains in a Forest and i do that in the following way:

foreach(Domain currDomain in Forest.GetCurrentForest().Domains)
{
    try
    {
        DirectorySearcher searcher = new
                         DirectorySearcher(currDomain.GetDirectoryEntry());
        searcher.PageSize = 1000;
        searcher.PropertiesToLoad.Add("cn");
        searcher.PropertiesToLoad.Add("distinguishedName");
        searcher.Filter = "(&(objectClass=group))";
        using (SearchResultCollection resList = searcher.FindAll())
        {
        }
    }
}

Now i want to do the same with SSL Enabled. To do that i tried to set the

AuthenticationType 

property in DirectoryEntry class to

AuthenticationTypes.SecureSocketsLayer.

But, when i try to execute, "An Operation Error Occured" exception is thrown.

Can someone please help me with this?

Upvotes: 1

Views: 2220

Answers (2)

Wow! I got this working with help from 's answer . Initially, i modified the code as given below with the help from the 1st answer:

DirectoryEntry de = new DirectoryEntry("LDAP://" + currDomain.Name + ":636")
DirectorySearcher searcher = new DirectoySearcher(searcher);

But still i got "An Operation Error Occured". Then i figured out, i have the following code as well:

de.AuthenticationType = AuthenticationTypes.SecureSocketLayer.

When i removed the above line, it started working fine and i confirmed it with WireShark.

Upvotes: 1

Gabriel Luci
Gabriel Luci

Reputation: 40858

Secure LDAP (LDAPS) listens on a different port (636) than regular LDAP (389). You need to tell it connect on that port. Try this:

DirectorySearcher searcher = new
                     DirectorySearcher(new DirectoryEntry("LDAP://" + currDomain.Name + ":636");

Upvotes: 1

Related Questions