title: How to Query LDAP over SSL from SQL Server
body: <p>Has anybody successfully queried LDAPS (LDAP over SSL/TLS) from SQL Server? How did you do it? </p>
  
  <p>A client has asked us to set up our program on a network that uses LDAPS. I’ve used LDAP queries successfully at many other locations, but it’s not clear whether this will work with LDAPS, or what the syntax is. I would expect it to work, but not sure how to set it up.</p>
  
  <p>As a test, we tried setting up LDAPS on our own network. We can connect using ldp.exe, but attempting to query it from SQL consistently fails with “Msg 7321, Level 16, State 2…”</p>
  
  <ol>
  <li>can a SQL query connect to LDAPS?</li>
  <li>Should I specify the port?</li>
  <li>Does the server certificate need to be imported into SQL Server, or elsewhere?</li>
  <li>Are there other extra setup steps needed beyond those normally needed for querying LDAP?</li>
  </ol>
  
  <p>Here’s the query that normally works, which I’m trying to modify for LDAPS.</p>
  
  <pre><code>SELECT COUNT(*) FROM OPENROWSET('ADSDSOObject', 'adsdatasource'; 
  'domain user here'; 'domain password here', 
      'SELECT SAMAccountName FROM 
  ''LDAP://192.168.0.105 
  WHERE SAMAccountName = ''test.test'' and objectClass = ''user'' 
          ')
  </code></pre>