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
jfz2004Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Looks like the second line to me. Lose the '<' maybe
CEHJCommented:
Sorry may have misread that
Giant2Commented:
See here for LDAP use examples:
http://javaalmanac.com/egs/javax.naming.ldap/pkg.html

Hope this could help you.

Bye, Giant.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Giant2Commented:
Giant2Commented:
Giant2Commented:
the following does'nt seems a valid URL
<LDAP://dc=myco,dc=com>\\reap_ldap
Giant2Commented:
and this last seems a strange username
CN=tmyid
Giant2Commented:
See here:
http://java.sun.com/products/jndi/tutorial/basics/prepare/initial.html
at Supply the Information Needed by the Initial Context

Hope all these things could help you.
Bye, Giant.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jfz2004Author Commented:
Thanks a lot,

I am reading the tutorials.

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

Thanks,

Jennifer
jfz2004Author Commented:
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
Giant2Commented:
com\reap_ldap
could be:
com/reap_ldap
jfz2004Author Commented:
let me try. Thanks,

Jennifer
jfz2004Author Commented:
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.


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

Hope this solve.
jfz2004Author Commented:
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


Giant2Commented:
Happy you solved.
tx accepting.
Bye, Giant.
jfz2004Author Commented:
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
Giant2Commented:
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))
jfz2004Author Commented:
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
jfz2004Author Commented:
I checked on my computer. I don't see
"Start > Programs > Ipswitch IM Server" in my start->programs.

Hmmm.

jfz2004Author Commented:
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
Giant2Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.