Results 1 to 8 of 8
  1. #1
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Question How to access Active Directory on Java ???

    i have two AD server's IP. I'm trying to get usernames from AD. first how to connect with Active Directory ???
    Last edited by qwerty53; 07-26-2011 at 12:23 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Google for Java LDAP tutorials.

  3. #3
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Google for Java LDAP tutorials.

    i have googled it and read all stuff about active directory.. it is really easy to say "look for it on google". i still have no answer. if you have any information about Acvite Directory and Java, could you help me please ?
    this is what i got on google, i have changed it and tried a lot of times; but it isn't work... i wrote the server id. it doesn'T connect. do u know why ????


    Java Code:
        public DirContext ActiveDirectory(String serverName, String username, String password) {
             DirContext dirContext = null;                      //  returned to the calling program
             Hashtable environment = new Hashtable();           //  holds parameters that will be passed to the AD server
    
             //  Set up the Active Directory environment.		
             environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
             environment.put(Context.PROVIDER_URL, "ldap://" + serverName);
             //environment.put(Context.SECURITY_AUTHENTICATION, "simple");
             //environment.put(Context.SECURITY_PRINCIPAL, username);
             //environment.put(Context.SECURITY_CREDENTIALS, password);
             environment.put(Context.REFERRAL, "follow");
    
             try {
                  dirContext = new InitialDirContext(environment);         //  Retrieve a "connection" to the Active Directory server.
             } catch (Exception ex) {
                  System.err.println(ex);
             }
    
             return dirContext;
        }
        
        public String getDnBySamAccountName(String networkUsername, String context, DirContext dctx) throws Exception {
         String subEntry;
    
         try {
              //  A SearchControls object holds information about the scope of a search.
              SearchControls controls = new SearchControls();
              controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
              //  searchString describes what we're looking for, a person, not a computer, with the specified name.
              String searchString = "(&(objectClass=person)(!(objectClass=computer))(sAMAccountName=" + networkUsername +"))";
    
              //  NamingEnumeration is an interface for lists returned by methods in the javax.naming and javax.naming.directory packages.
              //  In our case, it's going to hold the results of our search.
              NamingEnumeration enumer = dctx.search(context, searchString, controls);
    
              if (enumer!=null) {
                   subEntry = ((SearchResult) enumer.next()).getName();
                   return subEntry;
              }
         }
         catch (NullPointerException e) {
              
         }
         catch (Exception e) {
              System.err.println(e);
         }
         return null;
    }

  4. #4
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    this is the error message which i get:

    javax.naming.CommunicationException: ***.***.*.**:*** [Root exception is java.net.ConnectException: Connection timed out: connect]

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Presumably because you aren't giving it any login details?

  6. #6
    qwerty53's Avatar
    qwerty53 is offline Member
    Join Date
    Jul 2011
    Posts
    32
    Rep Power
    0

    Default

    password and username ???

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    I thought AD servers required you to logon.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Normally, yes.

    @OP, using the url and credentials given above can you access the server from outside your program.

Similar Threads

  1. Active Directory using RPC over HTTPS
    By eltmon in forum Advanced Java
    Replies: 0
    Last Post: 07-20-2011, 09:11 PM
  2. Creating a Group in Active Directory
    By Bumbsiii in forum Advanced Java
    Replies: 6
    Last Post: 01-17-2011, 09:09 AM
  3. Pulling from an Active Directory
    By Sno in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 10:15 PM
  4. Accessing Active Directory using JNDI
    By Chandran in forum Advanced Java
    Replies: 0
    Last Post: 02-25-2009, 01:04 PM
  5. Accessing Active Directory
    By javaplus in forum Advanced Java
    Replies: 1
    Last Post: 01-05-2008, 12:25 AM

Posting Permissions

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