i have two AD server's IP. I'm trying to get usernames from AD. first how to connect with Active Directory ???
Printable View
i have two AD server's IP. I'm trying to get usernames from AD. first how to connect with Active Directory ???
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 ????
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;
}
this is the error message which i get:
javax.naming.CommunicationException: ***.***.*.**:*** [Root exception is java.net.ConnectException: Connection timed out: connect]
Presumably because you aren't giving it any login details?
password and username ???
I thought AD servers required you to logon.
Normally, yes.
@OP, using the url and credentials given above can you access the server from outside your program.