Link to home
Start Free TrialLog in
Avatar of jfz2004
jfz2004

asked on

How to make Java use Microsoft's DirectoryServices?

Hi,

I can use .Net C# to use DirectoryServices; but I don't know how to use
Directory Services in my Java Middleware.

Does any one have sample Java code on using DirectoryServices?

My current Java code looks like:

Hashtable env = new Hashtable();      env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "<LDAP://dc=myco,dc=com>\\reap_ldap");
env.put(Context.SECURITY_PRINCIPAL,"CN=tmyid");

But, something is wrong with the third line.

Anyone knows why?

Thanks,

Jennifer
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Looks like the second line to me. Lose the '<' maybe
Sorry may have misread that
Avatar of Giant2
Giant2

See here for LDAP use examples:
http://javaalmanac.com/egs/javax.naming.ldap/pkg.html

Hope this could help you.

Bye, Giant.
the following does'nt seems a valid URL
<LDAP://dc=myco,dc=com>\\reap_ldap
and this last seems a strange username
CN=tmyid
ASKER CERTIFIED SOLUTION
Avatar of Giant2
Giant2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jfz2004

ASKER

Thanks a lot,

I am reading the tutorials.

The username "CN=tmyid" is indeed strange; but it is something
i am using.

Thanks,

Jennifer
Avatar of jfz2004

ASKER

still not working.
Here is the error I got:

Exception in thread "main" javax.naming.CommunicationException:
dc=******(which is my company name), dc=com\reap_ldap:389
[Root exception is java.net.UnknownHostException: dc=
******(which is again my company name), dc=com\reap_ldap

Any ideas?

Jennifer
com\reap_ldap
could be:
com/reap_ldap
Avatar of jfz2004

ASKER

let me try. Thanks,

Jennifer
Avatar of jfz2004

ASKER

Hi, I asked my company's support and he told me that:

ADS connection url : "<LDAP://dc=******(which is my company name),dc=com>"
User ID            : reap_ldap
PASSWORD           : abcedfg

So I am going to write my java code like this:


env.put(Context.PROVIDER_URL, "<LDAP://dc=******,dc=com>");
env.put(Context.SECURITY_PRINCIPAL, "reap_ldap");
env.put(Context.SECURITY_CREDENTIALS, "abcedfg ");

I will this and see what happens.


Ok. If not goes ok, try to remove the <...>, so
ADS connection url : "LDA://dc=........,dc=com"

Hope this solve.
Avatar of jfz2004

ASKER

Now my java code is like this:
env.put(Context.PROVIDER_URL, "LDAP://******.com");
env.put(Context.SECURITY_AUTHENTICATION,"none");      
env.put(Context.SECURITY_PRINCIPAL,"CN=reap_ldap");
env.put(Context.SECURITY_CREDENTIALS, "abcdefg");

and it works.

Thanks a lot for your help. Your tutorials are good.
http://java.sun.com/products/jndi/tutorial/basics/prepare/initial.html

Jennifer


Happy you solved.
tx accepting.
Bye, Giant.
Avatar of jfz2004

ASKER

I searched in the web and found that the cause of my problem is
that the DN specified in the User Search tab is wrong,
or incorrectly formatted. But I don't know what is should be the DN
and what is the User Search tab.

Anyone knows what is User Search tab or DN here?

Thanks,

Jennifer
See:
http://www.ipswitch.com/support/instant_messaging/guides/v2/IMServer/User_s_Guide/Getting_Started/Server/LDAP.htm

the example reported is:
Server Name
ldapserver.ipswitch.com

Port number
389

User Authorize DN
CN=%s,CN=Users,DC=ldapserver,DC=ipswitch,DC=com
 
User Search DN
CN=Users,DN=ldapserver,DC=ipswitch,DC=com
 
User Search Filter (All Users)
(|(objectClass=user)(objectClass=person))
 
LDAP User Search Filter (Single User)
(&(|(objectClass=user)(objectClass=person))(CN=%s))
Avatar of jfz2004

ASKER

Thanks,

I will look into that url.
But for now, I keep getting this error when I use getAttributes("samaccountname=t1234")
javax.naming.NamingException: [LDAP: error code 1 - 000020D6: SvcErr Problem 5012,  remaining name 'samaccountname=t1234'.

In a separated .Net C# example, getAttributes works fine, but in java it does not work.

I searched in the internet on ldap's error msg, and found that this error is caused
by:  the DN specified in the User Search tab is wrong, or incorrectly formatted.
But I don't understand it.

Ang sample code on using getAttributes?

Thanks,

Jennifer
Avatar of jfz2004

ASKER

I checked on my computer. I don't see
"Start > Programs > Ipswitch IM Server" in my start->programs.

Hmmm.

Avatar of jfz2004

ASKER

Hi,

In a DN like "cn=users,dc=company,dc=tld",
is cn really should be users always?

Should I replace users here with actual user name like "John Joe"?

Also, my company has something called 'samaccountname', is it
a common attributes?

Thanks,

Jennifer
samaccountname, mhmm. It's the first time I see it.
I found this thread speaking about it:
http://forum.java.sun.com/thread.jspa?threadID=517580&messageID=2469179