Need Help withAuthentication after password change.
Hi
I have to check always the user and his password against AD. the user would be using his email to login to the application. hence i need to get his principalName and then reauthenticate him. I am using a default user and password to search and get the principalName.
This small piece of code achieve it.
publicstaticvoid main(String[] args){
// Identify service provider to use
Control[] connCtls =null;
Hashtable env =new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,"ldap://192.168.0.18/dc=mydomain,dc=com");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"defaultUser@mydomain.com");
env.put(Context.SECURITY_CREDENTIALS,"p@ssw0rd");
DirContext ctx1 =null;
try
{
// Create the initial directory context
LdapContext ctx =new InitialLdapContext(env,connCtls);
SearchControls constraints =new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration e2 = ctx.search("","mail=testUser@myEmail.com",constraints);
while(e2.hasMoreElements())
{
System.out.println("LdapLookUp.main()-searched");
SearchResult nc = (SearchResult)e2.nextElement();
Attributes atrr = nc.getAttributes();
String userPrincipalName = (String)atrr.get("userPrincipalName").get();
System.out.println("main()-Atrr-"+userPrincipalName);
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, atrr.get("userPrincipalName").get());
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,"testp@ssw0rd");
ctx.reconnect(null);
System.out.println("main()-Reconnected");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
i run this class successfully, Now if the administrator resets the user password on AD and if i still run the same class using the old password it works fine... :(
I have tried using closing the context and then opening a new context also. Any suggestions regarding this would be very helpful

