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

Posted on 2011-05-11
Medium Priority
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

cmalakar earned 1000 total points
ID: 35743569
Yes, you should use ldap to connect to ActiveDirectory.

LVL 92

Assisted Solution

objects earned 1000 total points
ID: 35743586
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 35745175
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 = dirContext.search(searchBase, 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

ID: 35751082
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 60

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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