title: Contact Active Directory with SSL
body: <p>I have a method that validates user credentials against Active Directory. I would like to use this method with SSL but I can't get it to work.</p>
  
  <p>The main problem is that I have a server that are outside of our network (is it called DMZ?). And from there I wanna contact my active directory, and that's why I want to use SSL.</p>
  
  <p>When using this on my local computer (not from DMZ) I get this error:</p>
  
  <blockquote>
    <p>System.DirectoryServices.AccountManagement.PrincipalServerDownException: The server could not be contacted. --->    System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable.<br>
    at System.DirectoryServices.Protocols.LdapConnection.Connect()<br>
    at System.DirectoryServices.Protocols.LdapConnection.SendRequestHelper(DirectoryRequest request, Int32&amp; messageID)<br>
    at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout)<br>
    at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request)<br>
    at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties&amp; properties)  </p>
    
    <blockquote>
      <p>--- End of inner exception stack trace ---<br>
      at System.DirectoryServices.AccountManagement.PrincipalContext.ReadServerConfig(String serverName, ServerProperties&amp; properties)<br>
      at  System.DirectoryServices.AccountManagement.PrincipalContext.DoServerVerifyAndPropRetrieval()<br>
      at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options, String userName, String password)<br>
      at System.DirectoryServices.AccountManagement.PrincipalContext..ctor(ContextType contextType, String name, String container, ContextOptions options)<br>
      at Authorization.AuthorizeAD.ValidateCredentials(String username, String password)</p>
    </blockquote>
  </blockquote>
  
  <p>I figured that it would be good to get it working with SSL from local before I try it from our server.</p>
  
  <p>My method:</p>
  
  <pre><code>public bool ValidateCredentials(string username, string password) {
          using (
              var context = new PrincipalContext(ContextType.Domain, ContextName, ContextContainer,
                  ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing |
                  ContextOptions.SecureSocketLayer)) {
              return context.ValidateCredentials(username, password);
          }
      }
  </code></pre>
  
  <p>As said before, without <code>ContextOptions.SecureSocketLayer</code> it works fine (the other three are by default if parameter is null)</p>
  
  <p>Does anyone know how I should use <code>PrincipalContext</code> correct with SSL?</p>