How to Use Java to Query and Add a user in Active Directory

Posted on 2011-05-11
Last Modified: 2013-11-23
I can do this with simple ldap like Sun Directory Server fairly easy. Is it the same for Active Directory? What API's should I use if I want to create a custom connector to query, modify and add users to Active Directory. Does anyone have code samples?
Question by:tomsawyer213
    LVL 23

    Accepted Solution

    Yes, you should use ldap to connect to ActiveDirectory.
    LVL 23

    Expert Comment

    LVL 92

    Assisted Solution

    LVL 1

    Expert Comment

    Here is an example to query users:
    public static void importUsers(IDfSession session, String username, String password,String hostname, String port, String groupName, String domeniiCautare)
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://" + hostname+ ":"+port);
            env.put(Context.SECURITY_PRINCIPAL, username);
            env.put(Context.SECURITY_CREDENTIALS, password);
            env.put(Context.REFERRAL, "follow");
                DirContext  dirContext = new InitialDirContext(env);
                SearchControls ctls = new SearchControls();
                String returnedAtts[]={"mail","memberOf","CN"};
                String searchFilter = "(&(objectClass=user)(objectClass=person)(objectClass=organizationalPerson)(mail=*))";
                String searchBase = domeniiCautare;
                NamingEnumeration answer =, searchFilter, ctls);
                listResult(session, answer, groupName);
            catch (Exception e)
                log.error(e.getMessage(), e);
                System.out.print("Error on creating session! " + e.getStackTrace());

    Open in new window


    Author Comment

    How about this for an add? Where ctx = DirContext

     ModificationItem[] mods = new ModificationItem[1];
                mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("uid",uid));
                ctx.modifyAttributes(this.uid+",ou=People,ou=consumer", mods);
    LVL 59

    Expert Comment

    by:Kevin Cross
    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    how to extract window origin in spring 2 55
    MaxSpan challenge 9 50
    java JDK Download 7 49
    Receive file in Servlet 1 28
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now