I want to connect to LDAP server using my MVC application.I don't want to use active directory. Here is my post method when I get usename and password from user:
<pre lang="c#"> [HttpPost]
public ActionResult Login(Login model, string returnUrl)
{
string pwd = model.password;
System.Security.Cryptography.MD5 hs = System.Security.Cryptography.MD5.Create();
byte[] db = hs.ComputeHash(System.Text.Encoding.UTF8.GetBytes(pwd));
string result = Convert.ToBase64String(db);
var BuildServerName = new StringBuilder();
BuildServerName.Append("172.16.1.98");
var ldapConnection = new LdapConnection(BuildServerName.ToString());
var networkCredential = new NetworkCredential(model.username, result, "dc=mnit,dc=ac,dc=in");
ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
ldapConnection.AuthType = AuthType.Basic;
ldapConnection.SessionOptions.ProtocolVersion = 3;
ldapConnection.Bind(networkCredential);
It throws an error at
ldapConnection.Bind(networkCredential);
which is: The distinguished name contains invalid syntax. What should I do in this?
What I have tried:
When I change
BuildServerName.Append("172.16.1.98");
to
BuildServerName.Append("LDAP://172.16.1.98");
it returns error
The LDAP server is unavailable.
at
ldapConnection.Bind(networkCredential);
. Is there any other way to connect and check username and password through ldap.