title: Connect active directory with the support of ssl (ldaps)
body: <p>am trying to connect with active directory with the support of ssl.</p>
  
  <p>i tried the steps from following web site.</p>
  
  <p><a href="http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory" rel="nofollow">http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory</a></p>
  
  <p>when i try to connect active directory from the java code it gives following error.</p>
  
  <pre><code>Exception in thread "main" javax.naming.CommunicationException: simple bind fail
  ed: 172.16.12.4:636 [Root exception is java.net.SocketException: Connection rese
  t]
          at com.sun.jndi.ldap.LdapClient.authenticate(Unknown Source)
          at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
          at com.sun.jndi.ldap.LdapCtx.&lt;init&gt;(Unknown Source)
          at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
          at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
          at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
          at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
          at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
          at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
          at javax.naming.InitialContext.init(Unknown Source)
          at javax.naming.InitialContext.&lt;init&gt;(Unknown Source)
          at javax.naming.directory.InitialDirContext.&lt;init&gt;(Unknown Source)
          at ConnectActiveDirectory.main(ConnectActiveDirectory.java:39)
  Caused by: java.net.SocketException: Connection reset
          at java.net.SocketInputStream.read(Unknown Source)
          at com.sun.net.ssl.internal.ssl.InputRecord.readFully(Unknown Source)
          at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
          at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
  
          at com.sun.net.ssl.internal.ssl.SSLSocketImpl.waitForClose(Unknown Sourc
  e)
          at com.sun.net.ssl.internal.ssl.HandshakeOutStream.flush(Unknown Source)
  
          at com.sun.net.ssl.internal.ssl.Handshaker.kickstart(Unknown Source)
          at com.sun.net.ssl.internal.ssl.SSLSocketImpl.kickstartHandshake(Unknown
   Source)
          at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Un
  known Source)
          at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source
  )
          at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
          at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
          at java.io.BufferedOutputStream.flush(Unknown Source)
          at com.sun.jndi.ldap.Connection.writeRequest(Unknown Source)
          at com.sun.jndi.ldap.Connection.writeRequest(Unknown Source)
          at com.sun.jndi.ldap.LdapClient.ldapBind(Unknown Source)
          ... 13 more
  </code></pre>
  
  <p>Code that am using is</p>
  
  <pre><code>import java.util.Hashtable;
  import javax.naming.*;
  import javax.naming.directory.*;
  
  public class ConnectActiveDirectory {
  
      public static void main(String[] args) throws NamingException {
          Hashtable env = new Hashtable();
          env.put(Context.SECURITY_AUTHENTICATION, "simple");
          env.put(Context.SECURITY_PRINCIPAL,"Administrator@mysite.com");
          env.put(Context.SECURITY_CREDENTIALS, "password");
          env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
          env.put(Context.PROVIDER_URL, "ldaps://172.16.12.4:636/cn=Users,dc=mysite,dc=com");
          try{
              java.io.InputStream in = new java.io.FileInputStream("C:\\client.crt");
              java.security.cert.Certificate c = java.security.cert.CertificateFactory.getInstance("X.509").generateCertificate(in);
              java.security.KeyStore ks = java.security.KeyStore.getInstance("JKS");
              ks.load(null);
              if (!ks.containsAlias("alias ldap")) {
                  ks.setCertificateEntry("alias ldap", c);
              }
              java.io.OutputStream out = new java.io.FileOutputStream("C:\\keystorefile.jks");
              char[] kspass = "changeit".toCharArray();
              ks.store(out, kspass);
              out.close();        
          }catch(Exception e){
              e.printStackTrace();
          }
          System.setProperty("javax.net.ssl.trustStore", "C:\\keystorefile.jks");
          DirContext ctx = new InitialDirContext(env);
          NamingEnumeration enm = ctx.list("");
          while (enm.hasMore()) {
              System.out.println(enm.next());
          }
          ctx.close();
      }
  }
  </code></pre>
  
  <p>does am doing any mistake?</p>
  
  <p>where can i get good tutorial to do ssl connection with active directory ?</p>
  
  <p>does <a href="http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory" rel="nofollow">http://confluence.atlassian.com/display/CROWD/Configuring+an+SSL+Certificate+for+Microsoft+Active+Directory</a> site has correct steps to create and connect active directory with ssl?</p>
  
  <p>could any on please help me.</p>