File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Security and the fly likes Ldap newbie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Security
Bookmark "Ldap newbie" Watch "Ldap newbie" New topic
Author

Ldap newbie

Steve Sutton
Greenhorn

Joined: Aug 06, 2003
Posts: 7
I am new to ldap and I am having a hard time finding examples of what I need to accomplish. I am performing a search on a user and getting an attribute under that user. I want to get the value of that attribute and pass that along. Here is the code that I have.
DirContext ctx = null;
String hostURL = "ldap://ldaptest.nwa.com:389";
String passWord = "lwcsvc";


// Create a Hashtable object.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, hostURL);
env.put(Context.SECURITY_AUTHENTICATION, "simple" );
env.put(Context.SECURITY_PRINCIPAL, "cn=lwcsvc,ou=svcacct,dc=tads,dc=nwa,dc=com" );
env.put(Context.SECURITY_CREDENTIALS, passWord );

try
{
// Create the initial directory context

System.out.println(" Binding to the context");

ctx = new InitialDirContext(env);
}
catch (NamingException e)
{
System.err.println("Problem with Bind:" + e);
}

System.out.println("Successful Bind");
try
{
String baseDN = "CN=Users,DC=tads,DC=xxx,DC=com";
String searchFilter = "(CN=" + empID +")";
String[] attrID= {"memberOf"};

SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
ctls.setReturningAttributes(attrID);

NamingEnumeration answer = ctx.search(baseDN, searchFilter, ctls);
while (answer.hasMore())
{
SearchResult sr = (SearchResult)answer.next();
System.out.println(sr.toString() + "\n");
}
ctx.close();
}
catch (Exception e)
{
System.out.println("Could not find Group for empID: " +empID +e);
}
The result is:
"CN=c02188: null:null:{memberof
=memberOf: CN=monitorRecords,OU=groups,DC=tads,DC=xxx,DC=com}"
What I would really like to do is just retrieve the value of 'monitorRecords' instead of the entire string. How do I use the getAttribute function to do this after the search.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ldap newbie