title: Use SSL to connect to LDAP with .Net DirectoryServices
body: <p>I have some .NET working code (both as a desktop application and as a IIS deployment) to read data from LDAP:</p>
  
  <pre><code>string ldapUrl = "LDAP://myLdapUrl.example/ou=user,dc=MyDC";
  AuthenticationTypes auth = AuthenticationTypes.None;
  using (DirectoryEntry directoryEntry = new DirectoryEntry(
     ldapUrl,
     "cn=ldap_user,ou=user,dc=MyDC",
     "NotMyTruePassword",
     auth)
  {
     using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry))
     {
         directorySearcher.PropertiesToLoad.AddRange(new[] { "uid", "givenname", "sn", "middlename", "description", "memberof" });
         directorySearcher.Filter = String.Format("(&amp;(objectclass=person)(cn={0}))", user);
         directorySearcher.SearchScope = SearchScope.OneLevel;
         directorySearcher.SizeLimit = 10;
         SearchResult searchResult = directorySearcher.FindOne();
     }
  }
  </code></pre>
  
  <p>But when I try to connect to the LDAPS port (636), it fails with a </p>
  
  <blockquote>
    <p>System.Runtime.InteropServices.COMException (0x8007203A): Server is not operational.</p>
  </blockquote>
  
  <p>Considerations:</p>
  
  <ul>
  <li><p>I have added the server CA to my acount through MMC.</p></li>
  <li><p>After that, I can connect to the LDAPS port using LdapAdmin.</p></li>
  <li><p>I have tried the following changes:</p>
  
  <ol>
  <li><p>Just adding the port to the server URL<sup>1</sup>:</p>
  
  <pre><code>string ldapUrl = "LDAP://myLdapUrl.example:636/ou=user,dc=MyDC";
  </code></pre></li>
  <li><p>Adding the port and changing the authTypes to <code>SecureSocketsLayer</code><sup>2</sup>:</p>
  
  <pre><code>string ldapUrl = "LDAP://myLdapUrl.example:636/ou=user,dc=MyDC";
  AuthenticationTypes auth = AuthenticationTypes.SecureSocketsLayer;
  </code></pre></li>
  <li><p>Adding the port and changing the authType to <code>Secure</code><sup>2</sup>:</p>
  
  <pre><code>string ldapUrl = "LDAP://myLdapUrl.example:636/ou=user,dc=MyDC";
  AuthenticationTypes auth = AuthenticationTypes.Secure;
  </code></pre></li>
  </ol></li>
  </ul>
  
  <p>And I always get the same results.</p>
  
  <p>I have found some examples using directly the LDAP connections (from <code>System.DirectoryServices.Protocols</code>) but I would prefer not to change the code as I already got it working.</p>
  
  <p><hr/>
  <sup>1</sup> I often see some people claiming that I should change <code>LDAP://</code> for <code>LDAPS:</code>, but it seems that it is not how DirectoryServices works. And in any case that fails, too.</p>
  
  <p><sup>2</sup> I am pretty sure those two options are for authentication and not for setting up the SSL connection, but I have tried them anyway.</p>