Active Directory - Get users list from Active Directory using JAVA

Asked By Alexis Flores on 15-Sep-09 06:43 AM

I need the procedure to get users list from active directory using Java.

thanks

re - Lalit M replied to Alexis Flores on 15-Sep-09 06:55 AM

You can use System.getenv(String) method to get various enviromental variables, some of which include log on info. If you type 'set' on the commandline you will get a list of all enviromental variables.

For getting the active directory user list, you will have to use external tools (which you can execute via Runtime.getRuntime().exec(String) ).

Here's the snippet. - [)ia6l0 iii replied to Alexis Flores on 15-Sep-09 12:06 PM

As an alternative to what "Web Mavin" as suggested, you can use the javax.naming.ldap Package to retrieve the list of users from an active directory

Sample Snippet below:

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.;
import javax.naming.ldap.;
import javax.naming.*;
import java.util.Hashtable;
import java.util.Enumeration;

public class ActiveDirectoryHelper
{
public static void main(String[] args)
{
Hashtable envVars = new Hashtable();
envVars.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
envVars.put(Context.PROVIDER_URL, "ldap://" + servername);
envVars.put(Context.SECURITY_AUTHENTICATION, "simple");
envVars.put(Context.SECURITY_PRINCIPAL, "username");
envVars.put(Context.SECURITY_CREDENTIALS, "credentials!");

DirContext myContext = new InitialDirContext(envVars);

SearchControls searchCtrls = new SearchControls();
searchCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attributes = { "cn", "telephoneNumber", "sn", "userPrincipalName","memberOf","name" };
searchCtrls.setReturningAttributes(attributes);

String filter = "(objectClass=organizationalPerson)";
NamingEnumeration values = myContext.search("CN=Users,DC=domaincont,DC=com,DC=xx", filter, searchCtrls);

while (values.hasMoreElements())
{
SearchResult result = (SearchResult) values.next();
Attributes attribs = result.getAttributes();

if (null != attribs)
{
for (NamingEnumeration ae = attribs.getAll(); ae.hasMoreElements();)
{
Attribute atr = (Attribute) ae.next();
String attributeID = atr.getID();
for (
Enumeration vals = atr.getAll(); 
vals.hasMoreElements(); 
System.out.println(attributeID ": " vals.nextElement())
);
}
}
}

myContext.close();

}
catch (Exception e)
{
//e
}
}
}

re - Alexis Flores replied to [)ia6l0 iii on 18-Sep-09 07:20 AM

thanks for your help
Raghuraman replied to Alexis Flores on 26-Apr-11 05:27 AM

Iam able to extract the users for a particular group according to the given code snippet. However, i need to extract the first name and the last name for some of the users listed for that group. Please help me in getting the same. These are the attributes iam passing     String[] attributes = {"member","name","sn","givenName"}; but iam getting only users. If i print the name and surname, it is priting the null values ( System.out.println(attribs.get("givenname")); 
 System.out.println(attribs.get("sn")); ). Please do the needful