Results 1 to 3 of 3
  1. #1
    Ulrich is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Access Active Directory by LDAP requests delivers only one of a list of elements

    I'm trying to search a Microsoft Active Directory via LDAP-Access:

    Java Code:
    public class Access {
    	static InitialLdapContext dirCtx;
    	public static void main(String args[]){
    		try{
    			Hashtable env = new Hashtable();
    			env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    			env.put(Context.PROVIDER_URL, "ldap://<domain>:389");
    			env.put(Context.SECURITY_AUTHENTICATION, "simple");
    			env.put(Context.SECURITY_PRINCIPAL, "CN=u005078x1@tac-ad");
    			env.put(Context.SECURITY_CREDENTIALS, <password>);
    
    			dirCtx = new InitialLdapContext(env, null);
    
    			NamingEnumeration ne = null;
    			SearchControls controls =  new SearchControls();
    			
    			controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    			ne = dirCtx.search("OU=test-accounts,OU=administration,DC=tac-ad,DC=intra,DC=mycompany,DC=com","(CN=*)",controls);
    			if (ne != null) {
    				if (ne.hasMore()) {
    					SearchResult item = (SearchResult) ne.next();
    					display(item.getAttributes());
    				}
    
    			}
    		}
    		catch(javax.naming.AuthenticationException e){
    			e.printStackTrace();
    		}catch(NamingException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	private static void display(Attributes attr) throws NamingException{
    		NamingEnumeration ne = attr.getAll();
    		while(ne.hasMore()){
    			Attribute  obj = (Attribute)ne.next();
    			System.out.println(obj.getID()+"\t"+(String)obj.get(0));
    		}
    	}
    The result is just one element, though there more. I can see them with my LDAP Browser "Apache Directory Studio" an the very same userid.
    I I change row 18 to a specific requests
    Java Code:
    ne = dirCtx.search(<baseDN>,"(CN=u005078x1)",controls);
    or
    Java Code:
    ne = dirCtx.search(<baseDN>,"(CN=u810378x1)",controls);
    I get the correct result.
    It looks almost as if the server prohibits the setting "SCOPE_SUBTREE". But why do I get the full list, when accessing the directory with the LDAP Browser?

    The tree as displayed by the LDAP-Browser looks like this
    Java Code:
    DIT
      DC=tac-ad,DC=intra,DC=mycompany,DC=com
         OU=administration
            OU=test-accounts
                CN=u005078x1
                CN=u810378x1
                CN=....
    So any hint is appreciated.

    brgds,
    Ulrich
    Last edited by Ulrich; 01-02-2014 at 03:12 PM.

  2. #2
    Ulrich is offline Member
    Join Date
    Apr 2012
    Posts
    10
    Rep Power
    0

    Default Re: Access Active Directory by LDAP requests delivers only one of a list of elements

    Sorry, at last I found it: It's a stupid coding error. I did not code a loop when inspecting the results.
    Instead of "if (ne.hasMore()) {" it must read "while (ne.hasMore()) {"

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: Access Active Directory by LDAP requests delivers only one of a list of elements

    He he, let me guess: all it took was for you to actually explain the problem in words as well as code to someone else (in this case manifested by the creation of this forum post) and then something in your brain went "Uh wait a minute, what did I just say?" because the words and the code don't exactly match up.

    You'll find that this is not the last time that it will happen ;) It's a rule that an old colleague and mentor taught me and I still apply today: if you're stuck on a problem for 2 hours, stop what you're doing and show it to somebody else.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. LDAP - Active Directory Login with JNDI
    By bvilten in forum New To Java
    Replies: 3
    Last Post: 05-10-2013, 09:58 PM
  2. Replies: 0
    Last Post: 11-19-2011, 06:25 PM
  3. How to access Active Directory on Java ???
    By qwerty53 in forum New To Java
    Replies: 7
    Last Post: 07-27-2011, 01:50 PM
  4. Cant find myself in my companies ldap directory
    By richierich in forum Advanced Java
    Replies: 1
    Last Post: 04-05-2011, 05:03 AM
  5. Accessing Active Directory
    By javaplus in forum Advanced Java
    Replies: 1
    Last Post: 01-04-2008, 11:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •