How list out my domain users using LDAP

Hi all,

I am new LDAP server applications.

I want to list out all my local domain user list by using java & ldap. When i tried a sample application from this forum i found its working find but i am not getting any records to display.

public void importDomainUsers() throws UnknownHostException

{

InetAddress inet2 = InetAddress.getLocalHost();

String mydomain = inet2.getCanonicalHostName();

mydomain = serverName.substring(serverName.indexOf("."), serverName.lastIndexOf("."));

System.out.println("Host Name1 :"+inet2.getCanonicalHostName());

System.out.println("My Domain :"+mydomain);

Hashtable env = new Hashtable();

//Must use either the userPrincipalName or samAccountName,

//Cannot use the distinguished name

//String adminName = "Administrator@antipodes.com";

String adminName = userName;

String adminPassword = password;

String ldapURL = "ldap://"+serverName+":389";

System.out.println("ldap Url :"+ldapURL);

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

//set security credentials, note using DIGEST-MD5

//Requires user account to be stored with reversible encryption

env.put(Context.SECURITY_AUTHENTICATION,"DIGEST-MD5");

env.put(Context.SECURITY_PRINCIPAL,adminName);

env.put(Context.SECURITY_CREDENTIALS,adminPassword);

//Could also use DIGEST-MD5 to protect the communications

//Eg. auth-int;integrity, auth-conf;confidentiality

//env.put("javax.security.sasl.qop","auth-conf");

//And could also request the level of crypto

//Eg. low, medium, high

//env.put("javax.security.sasl.strength","high");

//connect to my domain controller

env.put(Context.PROVIDER_URL,ldapURL);

try {

// Create the initial directory context

DirContext ctx = new InitialLdapContext(env,null);

// Create the search controls

SearchControls searchCtls = new SearchControls();

//Specify the attributes to return

String returnedAtts[]={"sn","givenName","mail"};

searchCtls.setReturningAttributes(returnedAtts);

//Specify the search scope

searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

//specify the LDAP search filter

String searchFilter = "(&(objectClass=user)(mail=*))";

//Specify the Base for the search

String searchBase = "DC="+mydomain+",DC=com";

//initialize counter to total the results

int totalResults = 0;

// Search for objects using the filter

NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);

//Loop through the search results

while (answer.hasMoreElements()) {

SearchResult sr = (SearchResult)answer.next();

totalResults++;

System.out.println(">>>" + sr.getName());

// Print out some of the attributes, catch the exception if the attributes have no values

Attributes attrs = sr.getAttributes();

if (attrs != null) {

try {

System.out.println("mail: " + attrs.get("mail").get());

}

catch (NullPointerException e){

System.out.println();

}

}

}

System.out.println("Total results: " + totalResults);

ctx.close();

}

catch (NamingException e) {

System.err.println("Problem searching directory: " + e);

}

}

i used the above sample program, i passed our domain server as "server.eaglesoft.com and user name is "administrator" and password "*****" was given by the system admin.

when i run this program i m getting ,,

Total Results : 0

plz help me to solve the issue. my requirment is list out all the users userid and user type only..

regards,

dhaya.

[3803 byte] By [dhayasinfo@yahoo.co.ina] at [2007-11-26 19:57:24]
# 1

Nice to see code reuse !

Could be any number of problems.

1. The LDAP filter may not be returning any results. Perhaps none of your Active Directory users have a value for the mail attribute. Try using a search filter such as://specify the LDAP search filter

String searchFilter = "(objectClass=user)";

2. You may be specifiying the wrong search base. Check to make sure that your domain distinguished name is correct.//Specify the Base for the search

String searchBase = "DC="+mydomain+",DC=com";

An easy way of doing this is to check the values of the domainNamingContexts on the RootDSE. There was a post on this at http://forum.java.sun.com/thread.jspa?threadID=693373&tstart=0, alternatively just grab a copy of the Windows LDP.EXE tool, connect to your domain and check the rootDSE values.

If there was an error in your credentials, incorrect user name or password, then that should generate an LDAP authentication error. If you password is null, then that would imply an anonymous logon. By default, Active Directory does not return results to anonymous binds, but it seems as though your password is not null.

Good luck.

adler_stevena at 2007-7-9 22:52:08 > top of Java-index,Core,Core APIs...