Java and Active Directory

10. January 2007 11:25 by Mrojas in General  //  Tags:   //   Comments (0)
   Active Directory is also LDAP. So to integrate with the Active Directory you can use code very similar to that one used with other LDAP servers.

To connect to the Active Directory you can do something like this:

import java.util.Hashtable;
import
javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import
javax.naming.directory.InitialDirContext;

 /**
  * @author mrojas
  */

public class Test1
{

      public static void main(String[] args)
      {

            Hashtable environment = new Hashtable();
            //Just change your user here       
           
String myUser = "myUser";
            //Just change your user password here      

            String myPassword = "myUser"; 

            //Just change your domain name here

            String myDomain = "myDomain";

            //Host name or IP

            String myActiveDirectoryServer = "192.168.0.20";


            environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            environment.put(Context.PROVIDER_URL, "ldap://" + myActiveDirectoryServer + ":389");
            environment.put(Context.SECURITY_AUTHENTICATION, "simple");
            environment.put(Context.SECURITY_PRINCIPAL, "CN=" + myUser + ",CN=Users,DC=" + myDomain + ",DC=COM");
            environment.put(Context.SECURITY_CREDENTIALS, myPassword);
            try
           
{
                  DirContext context = new InitialDirContext(environment);
                 
System.out.println("Lluvia de exitos!!");
           
}
            catch (NamingException e)
            {
                  e.printStackTrace();
            }
      }
}