Click here to Skip to main content
15,125,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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");


        // setup an ldapconnection to that endpoint
        var ldapConnection = new LdapConnection(BuildServerName.ToString());

        // it looks like you have an administrative account to bind with, so use that here
        var networkCredential = new NetworkCredential(model.username, result, "dc=mnit,dc=ac,dc=in");

        // set the following to true if it's over ssl (636), if not just set it to false

        ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };

        // now set your auth type - I typically use 'negotiate' over LDAPS, and `simple` over LDAP
        // for this example we'll just say you're not using LDAPS
        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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900