karthik i ran the code you gave me but i am getting the following error:Here is my Stdout
Context Sucessfully Initialized
javax.naming.OperationNotSupportedException: [LDAP: error code 53 - referral missing]; remaining name 'uid=defaultuser'
And here is my code:
/*
* Created on Dec 8, 2003
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author root
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.NamingException;
import java.util.Hashtable;
public class LdapExampleAdd {
public static void main(
String[] args)
{
//Identify service provider to use
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "dc=webselfcare, dc=com");
env.put(Context.SECURITY_CREDENTIALS, "takecare");
try
{
// Create the initial directory context
InitialDirContext initialContext = new InitialDirContext(env);
DirContext dCtx = (DirContext)initialContext;
System.out.println("Context Sucessfully Initialized");
Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("uid", "defaultuser"));
matchAttrs.put(new BasicAttribute("cn", "defaultuser"));
matchAttrs.put(new BasicAttribute("givenname", "defaultuser"));
matchAttrs.put(new BasicAttribute("sn", "defaultuser"));
matchAttrs.put(new BasicAttribute("userpassword", "password"));
matchAttrs.put(new BasicAttribute("objectclass", "top"));
matchAttrs.put(new BasicAttribute("objectclass", "person"));
matchAttrs.put(new BasicAttribute("objectclass", "organizationalPerson"));
matchAttrs.put(new BasicAttribute("objectclass","inetorgperson"));
String name="uid=defaultuser";
InitialDirContext iniDirContext = (InitialDirContext)dCtx;
iniDirContext.bind(name,dCtx,matchAttrs);
iniDirContext.close();
dCtx.close();
}
catch (NamingException ne)
{
System.err.println(ne);
}
catch(Exception e)
{
System.err.println(e);
}
}
}
Whats wrong here karthik?