#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    42
    Rep Power
    5

    LDAP Search in JAVA


    Hi Guys, need help. What I am trying to do here is to connect to a LDAP server with Base DN="ou=Users Data,o=userINFO,ou=userDEV,O=user-D".

    Then by using the directory "Users Data", would like to search the user given UserName (from a login page) just to verify whether this UserName is existed in this "Users Data" directory or not.

    I would like to do this kind of search in JAVA.

    Can any one please guide me through? Oh note that the Base DN is a public domain means anyone can search the data as "Anonymous".
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    42
    Rep Power
    5
    Please look through what i did so far but still having problem. Please help:

    Code:
    		Hashtable Env = new Hashtable();
    		
    		Env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    		
    		String url = "ldap://XX.XX.XX.XX:XXX/";
    		Env.put(Context.PROVIDER_URL, url);
    		
    		try {
    		    DirContext dctx = new InitialDirContext(Env);
    			
    		    SearchControls sc = new SearchControls();
    		    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
    			
    			String filter = "(userid="+userName+")";
    			
    			String base="ou=Users Data,o=userINFO,ou=userDEV,O=user-D";
    	
    			NamingEnumeration results = dctx.search(base, filter, sc);
    
    		  // Here i would like to implement if the "userName" is there then say "True" or "False"
    		  // But don't know how :(
    	        
    			System.out.print(results.toString());
    
    		    // Close the context when we're done
    		    dctx.close();
    		} catch (NamingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0
    Originally Posted by faisalnet7
    Please look through what i did so far but still having problem. Please help:

    Code:
    		Hashtable Env = new Hashtable();
    		
    		Env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    		
    		String url = "ldap://XX.XX.XX.XX:XXX/";
    		Env.put(Context.PROVIDER_URL, url);
    		
    		try {
    		    DirContext dctx = new InitialDirContext(Env);
    			
    		    SearchControls sc = new SearchControls();
    		    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
    			
    			String filter = "(userid="+userName+")";
    			
    			String base="ou=Users Data,o=userINFO,ou=userDEV,O=user-D";
    	
    			NamingEnumeration results = dctx.search(base, filter, sc);
    
    		  // Here i would like to implement if the "userName" is there then say "True" or "False"
    		  // But don't know how :(
    	        
    			System.out.print(results.toString());
    
    		    // Close the context when we're done
    		    dctx.close();
    		} catch (NamingException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    We have some samples for JNDI here: http://ldapwiki.willeke.com/wiki/JNDISamples

    -jim

IMN logo majestic logo threadwatch logo seochat tools logo