Results 1 to 2 of 2
  1. #1
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default Pulling from an Active Directory

    Hey guys,

    So, I am required to pull from an active directory.

    I have already been able to complete an authorization test into the directory, but I am having trouble searching the active directory for the attributes I need.

    I am playing with an example from sun which is located here.

    Naming and Directory (JNDI) - JNDI, Active Directory, Referrals and Global Catalog [Locked]

    How does SearchFilter work?

    PHP Code:
    String searchFilter = "(title=intern)";
    String searchBase = "";
     
    //initialize counter to total the results
    int totalResults = 0;
     
     
    //Search for objects in the GC using the filter
    NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
    When I run this code inside my Authorization

    PHP Code:
    ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,username);
    ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
    ctx.reconnect(connCtls);
    System.out.println(username + " is authenticated");
    return true;
    it works, but breaks together for
    PHP Code:
    ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,username);
    ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
    ctx.reconnect(connCtls);
    String searchFilter = "(title=intern)";
    String searchBase = "";
     
    //initialize counter to total the results
    int totalResults = 0;
     
     
    //Search for objects in the GC using the filter
    NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
    System.out.println(username + " is authenticated");
    return true;
    Last edited by Sno; 09-24-2010 at 08:55 PM.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Solved, Here is the solution.

    PHP Code:
    import java.util.Hashtable;
    import javax.naming.*;
    import javax.naming.ldap.*;
    import javax.naming.directory.*;
    
    public class ldapfastbind {
        public Hashtable env = null;
        public LdapContext ctx = null;
        public Control[] connCtls = null;
     
        public ldapfastbind(String ldapurl) {
            env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION,"simple");
            env.put(Context.PROVIDER_URL,ldapurl);
     
            connCtls = new Control[] {new FastBindConnectionControl()};
     
            //first time we initialize the context, no credentials are supplied
            //therefore it is an anonymous bind.        
     
            try {
                ctx = new InitialLdapContext(env,connCtls);
     
            }
            catch (NamingException e) {
                System.out.println("Naming exception " + e);
            }
        }
        public boolean Authenticate(String username, String password) {
            try {
                ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,username);
                ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
                ctx.reconnect(connCtls);
                /*******************************************************************/
                SearchControls searchCtls = new SearchControls();
                
                //Specify the attributes to return
                String returnedAtts[]={"CN","mail","telephoneNumber"};
                searchCtls.setReturningAttributes(returnedAtts);
            try{
                //Specify the search scope
                searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                
                String searchFilter = "(sAMAccountName=jhelmbre)";
                 
                //Specify the Base for the search
                //an empty dn for all objects from all domains in the forest
                String searchBase = "";
     
                //initialize counter to total the results
                int totalResults = 0;
     
     
                //Search for objects in the GC using the filter
                NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
                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();
                    System.out.println(attrs.toString());
                //    System.out.println(attrs.getAll().toString());
                    }
            }catch(Exception e){e.printStackTrace();}
                /*******************************************************************/
                //System.out.println("Domain Name:"+ attri.toString());
                System.out.println(username + " is authenticated");
                return true;
            }
     
            catch (AuthenticationException e) {
                System.out.println(username + " is not authenticated");
                return false;
            }
            catch (NamingException e) {
                System.out.println(username + " is not authenticated");
                return false;
            }
        }
        public void finito() {
            try {
                ctx.close();
                System.out.println("Context is closed");
            }
            catch (NamingException e) {
                System.out.println("Context close failure " + e);
            }
        }
        
        
    }
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

Similar Threads

  1. active window
    By yuhobebbho in forum New To Java
    Replies: 3
    Last Post: 08-18-2010, 03:34 PM
  2. active jtable
    By water in forum AWT / Swing
    Replies: 0
    Last Post: 11-06-2009, 04:04 AM
  3. Accessing Active Directory using JNDI
    By Chandran in forum Advanced Java
    Replies: 0
    Last Post: 02-25-2009, 12:04 PM
  4. Close Active Window (?)
    By dcnorman07 in forum New To Java
    Replies: 2
    Last Post: 10-03-2008, 05:55 PM
  5. Accessing Active Directory
    By javaplus in forum Advanced Java
    Replies: 1
    Last Post: 01-04-2008, 11:25 PM

Posting Permissions

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