Query LDAP userid through Java

Java 6, JNDI:
I am logged in to a corporate LDAP server as a normal user and would like to query other users. My primary objective is to query LDAP for a Common Name based on the provided user id from a List. I can query my general info successfully, although cannot return my uid. Is there anything wrong with this query?  Below is a code snippet of the query setup.

Code snippet:
-----------------
 try {
            ctx = connect(managerName, managerPassword);

            String filter = "(objectclass=Person)";

            // ceate default search controls
            SearchControls controls = new SearchControls();

            String[] attrId =  {"sn", "givenname", "cn", "ou", "uid", "memberOf"};
            controls.setReturningAttributes(attrId);
           controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            NamingEnumeration<SearchResult> answer = ctx.search(bindDn, filter, controls);
            isValid = true;
            while (answer.hasMore()) {
                SearchResult sr = (SearchResult) answer.next();
                Attributes attr = sr.getAttributes();
                System.out.println("Attributes:" + attr);
            }
-------------------------
Returned Attributes are populated with CN, DC, OU, SN, and so on but no UID:
rayskeltonAsked:
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.

VenabiliCommented:
Stupid question but... is there uid in the LDAP for this user?
0
rayskeltonAuthor Commented:
No, the question is not stupid; especially since the uid name is  "sAMAccountName". With this name, I can query my user id so now the challenge is to query others id. I had a special account setup that have the permissions to query others so how is this done?
0
VenabiliCommented:
So this is an Active Directory? Yes - you have sAMAccountName here then, not uid for identification purposes:)

I am not sure I understand the question now?
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

rayskeltonAuthor Commented:
A query through JNDI to lookup user id from Common Name.
0
VenabiliCommented:
In the same LDAP? The way you have now should work (except that the filter should contain the cn also)...

Sorry - it is either getting too late for me or I really do not understand what problem you have now :)
0
rayskeltonAuthor Commented:
Yes, I have a List to of login ids(sAMAccountName) to feed a query to LDAP for the Common Name.  The code snippet is listed above and should be a relative easy task. I can handle the Java, just need the LDAP specifics related to the query.
0
VenabiliCommented:
Morning :)

So you have the sAMAccountName and you need to get the Common Name (this is how I read your latest message) or do you have the common name and you need to get the sAMAccountName?
0
rayskeltonAuthor Commented:
Yes, that is correct.
0
VenabiliCommented:
Which of the two? :)
0
rayskeltonAuthor Commented:
After connected to server through a JNDI connect, I resolved the problem myself with the following code

BasicAttributes basAttr = new BasicAttributes();
            basAttr.put("sAMAccountName", "otheruserid");

           // This was the key to this question
           NamingEnumeration<SearchResult> reply = ctx.search("OU=Users, OU=xxxx, DC=xxx, DC=company, DC=net",  basAttr);

             while (reply.hasMore()) {
SearchResult sr = (SearchResult) reply.next();
                System.out.println("Search Results:" + sr.toString());
                Attributes attr = sr.getAttributes();
                Attribute uid = attr.get("sAMAccountName");
                Attribute name = attr.get("distinguishedName");
                System.out.println("uid:" + uid + "Name:" + name);                
            }
0
VenabiliCommented:
Good work :)
0

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
rayskeltonAuthor Commented:
Solved the problem myself and need to close this question.
0
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.