Saturday, March 28, 2009

Authentication Using LDAP

Intitally I tried to bind to the LDAP using admin password in the DirectoryEntry constructor and using  the Filter to authenticate usergiven userid and password. But failed as couldnot access UserPassword attribute.

Later I used Userid and password directly in the DirectoryEntry constructor. 

Limitation - Clear Text password. Need to fix it.

DirectoryEntry entry = new DirectoryEntry(LDAP://164.XXX.XX.X/o=XXX.in,dc=XXX,dc=in);

entry.Username ="uid="+email.Text+",ou=people,o=XXX Employees,o=XXX Support,o=XXX.in,dc=XXX,dc=in"; //email text box contains the user id and password text contains the password

entry.Password = password.Text;

entry.AuthenticationType = AuthenticationTypes.None;

DirectorySearcher searcher = new DirectorySearcher(entry);

try

{

searcher.Filter = "(uid=" + email.Text + ")";

SearchResult rs = searcher.FindOne();

if (rs != null)

{

//success

divOutput.InnerHtml="";

divOutput.InnerHtml = "Welcome - " + rs.Properties["cn"][0].ToString();

}

else

{

//Failure

divOutput.InnerHtml = "Error authenticating. Check Your User Id or Password";

}

}

catch (Exception ex)

{

divOutput.InnerHtml = "Error authenticating. " + ex.Message;

return;

}

No comments: