Helpful Information
 
 
Category: LDAP Programming
Active Directory Services using Java API

Hello All,

I want to know whether its possible to connect to Active Directory Services through Java and able to retreive the users and groups from Active Directory Services. If its possible can i get the code.

thanks
Winston

did you get any solution ?

I am interested too.

Thanks.

Antheo.

AD exposes an LDAP interface, so it could be possible to connect to like any other LDAP server.
On Sourceforge there is a specific taglib, other material on OpenLDAP.

i searched on sourceforge and openldap regarding the ldap interface for active directory.

i found nothing on sourceforge... and the only link from openldap was to novell. i've been trying to get it for sometime, but novell does not allow download accelerators nor continuation applications (it seems) and i've been "timed out" way too many times, that frustration has built up.

i would like pabloj to be more specific in his post, or maybe provide a link as to where his two direction is? or if novell is correct and worth me trying to get it downloaded, as i've gotten the netscape ldap sdk only find it is specifically for developing with netscape (it seems..)

please? thx

I succeedeed connecting to AD by using the jndi tutorial from Sun.
Take a look at it.

thx antheo! found the stuff and looks workable.. trying it out! :)

i've run into problems again. i've created a user account "john doe", and did not place it into any groups. right now, i keep gettting:
javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C09030B, comment: AcceptSecurityContext error, data 525, v893

there is some sort of authentication problem here. i'm using the following:

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://"+ address:port+"/dc=domain,dc=name,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=john doe,dc=domain,dc=name,dc=com");
env.put(Context.SECURITY_CREDENTIALS, password);

i've also tried with "ou=user" after "cn=doe" to no avail. i'm connecting to win2k server with service pack 3 installed, and i can get a connection using LDAPbrowser (from Jarek Gawor) using the same values.

I succeedeed connecting to AD by using the jndi tutorial from Sun.
Take a look at it.
Hello,

Can you please send me the code to connect to AD using JNDI.

Also, please let me know which jar file needs to be put in the classpath.

Thanks in advance.
Andy

Hi Shift244, sorry for the delay.
I was referring to:
http://www.openldap.org/jldap/
http://www.openldap.org/jdbcldap/
http://opensource.socialchange.net.au/ldaptaglib/

Thanks guys, I was looking for a solution to a similar problem.

Hello,

Can nayone please send me the code to connect to AD using java code .

Also, please let me know which jar file needs to be put in the classpath.

Thanks in advance.
Alvi :)

Nemath you should start a new thread makes it alot easier to find your post.

Hi,



import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;
class DomainCon
{
public static void main(String args[])
{
Hashtable env= new Hashtable(11);
env.put(Context.SECURITY_AUTHENTICATION,"none");
env.put(Context.SECURITY_PRINCIPAL,"CN=kiran,OU=LinkedgeOU,DC=LINKEDGEDOMAIN");//User
env.put(Context.SECURITY_CREDENTIALS, "kiran");//Password
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://testing2:389/DC=LINKEDGEDOMAIN");
try
{
DirContext ctx = new InitialDirContext(env);
String[] sAttrIDs = new String[2];

Attributes attr = ctx.getAttributes("");
System.out.println("Domain Name:"+ attr.get("name").get());
}
catch(NamingException e)
{
System.err.println("Problem getting attribute: " + e);
}
}
}

This java code will help you ou to connect with active directory

Winston..
Please read this article .
http://forum.java.sun.com/thread.jspa?threadID=581444&messageID=3313188




Hello All,

I want to know whether its possible to connect to Active Directory Services through Java and able to retreive the users and groups from Active Directory Services. If its possible can i get the code.

thanks
Winston

I succeedeed connecting to AD by using the jndi tutorial from Sun.
Take a look at it.

Hello,
This is Bharadwaj. can you please guide me, where you found the tutorial for connecting to Active Directory using JAVA programming. Can you please gimme the exact link for that tutorial. I need to finish this task given by my PM urgently.
Please help me.

Thank You

Hello Winston,
I had the same doubt which u had. So, please post me the solution.Its urgent Mr.Winston. My boss has given me the task of accessing the information in Active Directory using JAVA programming. So, can you please help me regarding this? My job is at stake here.
It would be grateful, if you could reply to the email id as soon as possible.

Hello All,

I want to know whether its possible to connect to Active Directory Services through Java and able to retreive the users and groups from Active Directory Services. If its possible can i get the code.

thanks
Winston

Hello Winston,
I had the same doubt which u had. So, please post me the solution.Its urgent Mr.Winston. My boss has given me the task of accessing & modifying the information in Active Directory using JAVA programming. So, can you please help me regarding this? My job is at stake here.
It would be grateful, if you could reply to the email id as soon as possible.

Hello All,

I want to know whether its possible to connect to Active Directory Services through Java and able to retreive the users and groups from Active Directory Services. If its possible can i get the code.

thanks
Winston

ya my friend, Windston, Its possible :) , by JNDI lookup, you have only LDAP configured into you Microsoft ADS.

for complete code and help please visit my blog or sen me buzz on my Email (hitesh.rup@gmail.com) or you can visit my Blog

this code is working and not a single error. ya some improvement needed based on your requirements. Please free to buzz me when you need help.

Enjoy !!! the power of OpenSource with Java :)

my friend even you can use ADS server information using java program.

for complete code and detail, visit my blog, where you can find whole code.

I am new to LDAP and AD. Please help me understand what I am doing wrong.

I have used the above code and modified to my own server, domain, etc.

It seems I am missing something. I am trying to non-anonymously connect to the AD. So, in detail:

1 - I have a jsp and a servlet. I have a class with the login for ldap. Servlet calls ldap to connect to AD.
2 - In my ldap class, I have the credentials supplied, and I am searching for a username and a password. As of right now, I have creds supplied for testing. I would like to eventually be able to go all the way to non-anon.
3 - Problem: username and password are hard-coded in and matched in the page prompt. I get an error saying username is null. I have debugged and, sure enough, it's null. It should be auto-set considering it was hardcoded in.
4 - another problem is the ldap bind. There is a problem binding since I can't access the active directory.

Here is the code I have for the LDAP class. Please help me.







private transient String userLogin;
private transient String userPassword;
User user;

Hashtable<String, String> envGC = new Hashtable<String, String>();
Hashtable<String, String> envDC = new Hashtable<String, String>();

public LDAPCrap() throws NamingException {

// envGC = new Hashtable();
// envDC = new Hashtable();

String urlGC = "ldap://ssc-gc-01.mycompany.com:3268";
String urlDC = "ldap://ssc-dc-01.ssc.mycompany.com:389";

envGC.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
envDC.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");

envGC.put(Context.PROVIDER_URL, urlGC);
envDC.put(Context.PROVIDER_URL, urlDC);
}

public void setUserLogin(String uid) {
this.userLogin = uid;
}

public void setUserPass(String password) {
this.userPassword = password;
}

public boolean isAuth() throws NamingException {

DirContext ctxGC = new InitialDirContext(envGC);
DirContext ctxDC = new InitialDirContext(envDC);

boolean auth = false;
try {

ctxDC.getNameInNamespace();
ctxGC.getNameInNamespace();

// String userDN = getDN(this.userLogin);

envGC.put(Context.SECURITY_AUTHENTICATION, "simple");
envGC.put(Context.SECURITY_PRINCIPAL,
"cn=username,cn=Users,dc=ssc,dc=mycompany,dc=com");
// envGC.put(Context.SECURITY_PRINCIPAL, "cn=" + this.userLogin
// + "cn=Users,dc=ssc,dc=mycompany,dc=com");
envGC.put(Context.SECURITY_CREDENTIALS, this.userPassword);

envDC.put(Context.SECURITY_AUTHENTICATION, "simple");
envDC.put(Context.SECURITY_PRINCIPAL,
"cn=username,cn=Users,dc=ssc,dc=mycompany,dc=com");
// envDC.put(Context.SECURITY_PRINCIPAL, "cn=" + this.userLogin
// + "cn=Users,dc=ssc,dc=mycompany,dc=com");
envDC.put(Context.SECURITY_CREDENTIALS, "password");

envDC.put(Context.REFERRAL, "follow");
envGC.put(Context.REFERRAL, "follow");

auth = true;

} catch (AuthenticationException e) {

auth = false;
} catch (NamingException e) {
auth = false;
System.out.println("Error in Authentication " + e);
}

String base = "";
int totalResults = 0;
String filter = "(objectclass=*)";

ctxDC.bind(userLogin, user);
ctxGC.bind(userLogin, user);

SearchControls controls = new SearchControls();

// may be a scope change
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

NamingEnumeration<?> answer = ctxGC.search(base, filter, controls);

// ... process attributes ...

while (answer.hasMoreElements()) {

SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();

System.out.println("RootDSE: " + sr.getName());

if (attrs != null) {

try {

System.out.println(" Naming Context: "
+ attrs.get("defaultNamingContext").get());

System.out.println(" Schema Context: "
+ attrs.get("schemaNamingContext").get());

System.out.println(" DNS: "
+ attrs.get("dnsHostName").get());

System.out.println(" Server Name: "
+ attrs.get("serverName").get());

System.out
.println(" name(GC) Context: "

+ attrs.get("givenName").get() + ""
+ attrs.get("sn").get());

System.out.println(" mail(GC) Context: "
+ attrs.get("mail").get());

} catch (NullPointerException e) {
System.err
.println("Problem listing attributes from Global Catalog: "
+ e);
}
}

Attributes DCattrs = ctxDC.getAttributes(sr.getName());
try {
System.out.println(" Web(DC_):"
+ DCattrs.get("wWWHomePage").getID());
System.out.println(" Fax(DC):"
+ DCattrs.get("facsimileTelphoneNumber").getID());
// } catch (NamingException e) {
//
// System.out.println("Problem retrieving RootDSE: " + e);

} catch (NullPointerException e) {
System.err.println("Problem listing attributes from Domain "
+ " Controller:" + e);
}

try {
System.out.println("Total Results:" + totalResults);
ctxDC.unbind(userLogin);
ctxGC.unbind(userLogin);
ctxDC.close();
ctxGC.close();
} catch (Exception e) {
System.out.println("FAILED: " + e.getLocalizedMessage());

}

}
return (auth);
}


Any direction is appreciated.










privacy (GDPR)