Search code examples
javaactive-directoryldapadamadldap

Error when trying to list of users from active directory (AD LDS)


I'm trying to get a list of all users from my active directory (AD LDS/ADAM) However, I keep getting the following error:

Lookup failed: javax.naming.NameNotFoundException: [LDAP: error code 32 - 000020 8D: NameErr: DSID-031522C9, problem 2001 (NO_OBJECT), data 0, best match of: 'DC=PORTAL,DC=COMPANY,DC=BE'

My code:

public static void main(String[] args) {
        try {
            DirContext ctx = new InitialDirContext(Environment.getEnvironment());
            NamingEnumeration enumeration = ctx
                    .list("OU=ACCOUNTS,DC=PORTAL,DC=COMPANY,DC=BE");

            while (enumeration.hasMore()) {
                NameClassPair nc = (NameClassPair) enumeration.next();
                System.out.println(enumeration);
            }

            // Close the context when we're done
            ctx.close();

        } catch (AuthenticationException e) {
            System.out.println("Invalid credentials");
        } catch (NamingException e) {
            System.out.println("Lookup failed: " + e);
        }
    }

EDIT: added connection details

public static Hashtable getEnvironment() {
        // Set up the environment for creating the initial context
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:389/");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");

        env.put(Context.SECURITY_PRINCIPAL,
                "CN=Admin,OU=System Accounts,DC=PORTAL,DC=COMPANY,DC=BE");
        env.put(Context.SECURITY_CREDENTIALS, "Pass123");
        env.put(Context.REFERRAL, "follow");

        return env;
    }

Solution

  • got it to work with this code:

    NamingEnumeration enumResult = context.search(
                 "DC=PORTAl,DC=COMPANY,DC=BE", "(CN=*)",
                 controls);