title: Connect to Active Directory using Java JNDI
body: <p>I am new to using JNDI and I am trying to connect to Active Directory using JNDI and I am facing either Authentication Error or Connection Time out. I am unable to understand what is the potential reason.This how my Active Directory looks like </p>
  
  <p><a href="https://i.stack.imgur.com/Rrkp4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rrkp4.png" alt="enter image description here"></a></p>
  
  <p>I have tried the following code</p>
  
  <pre><code>public class ConnectAD {
  
  static DirContext ldapContext;
  
  public static void main(String[] args) throws NamingException {
      try {
          System.out.println("Début du test Active Directory");
  
          Hashtable&lt;String, String&gt; ldapEnv = new Hashtable&lt;String, String&gt;();
          ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
          ldapEnv.put(Context.PROVIDER_URL, "ldap://172.16.1.179:389");
          ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
          ldapEnv.put(Context.SECURITY_PRINCIPAL, "ou=Users,ou=Test1,dc=gigabyte,dc=local");
          ldapEnv.put(Context.SECURITY_CREDENTIALS, "5uperCharger");          
  
           ldapContext = new InitialDirContext(ldapEnv);
          //LdapContext ctx = new InitialLdapContext(ldapEnv,null);
  </code></pre>
  
  <p>I get the error at while creating the InitialDirContext.</p>
  
  <p>I have an administrator user but I tried giving the cn=administrator but could not connect. I was getting an Authentication Error when I do so.</p>
  
  <p>I also have a name to my ADServer which is GIGA(just trying to provide as much as i can)
  Can you please let me know what can be the issue.</p>