LDAP authentication and get all user data in C# / MVC 5

0

I've LDAP credentials, those are

  • LDAP Host: ldap.abcd.net
  • LDAP Port: 636
  • Use SSL
  • Base DN: dc=testbd,dc=net
  • User: uid=hris,ou=People,dc=testbd,dc=net
  • Password: abc123

using this I can connect and get all users by using a software named "Softerra LDAP Administrator", but while using C#/ .net MVC I've used this code which gives "Unknown error (0x80005000)" while search.FindAll();. I have also tried without Port number.

 DirectoryEntry myLdapConnection = new DirectoryEntry("LDAPS://ldap.abcd.net:636/", "uid=hris,ou=People,dc=testbd,dc=net", "abc123");

         DirectorySearcher search = new DirectorySearcher(myLdapConnection) { Filter = ("(objectClass=*)") };
         search.CacheResults = true;
         SearchResultCollection allResults = search.FindAll(); //Getting "Unknown error (0x80005000)"

I've tried another way it also gives "Unknown error (0x80005000)"

DirectoryEntry nRoot = new DirectoryEntry("LDAPS://ldap.abcd.net:636/ou=People,dc=testbd,dc=net");
        nRoot.AuthenticationType = AuthenticationTypes.None;
        nRoot.Username = "uid=hris,ou=Group,dc=testbd,dc=net";  //full dn

        nRoot.Password = "abc123";

        DirectorySearcher nDS = new DirectorySearcher(nRoot);
        nDS.SearchScope = SearchScope.Subtree;
        nDS.Filter = "objectClass=*";

        SearchResult sr = nDS.FindOne();// Getting ""Unknown error (0x80005000)"

I've tried many other ways but not getting proper data that software "Softerra LDAP Administrator" getting. Please help me through that I can all user information from LDAP and validate one user data. Thank you.

c#
asp.net
asp.net-mvc
ldap
asked on Stack Overflow Jan 18, 2017 by Imtiaz Rifat

1 Answer

0

THE SOLUTION

             var ldapDomainName= "LDAP://ldap.xserver.net";
             var loginId = "testUser";
             var password = "aa1111";
                       // assign user
                        var uid = "uid=" + loginId.Trim() + ",ou=People,dc=servicenginebd,dc=net";
                        // assign password
                        var passwordLdap = password;
                        // define LDAP connection
                        var root = new DirectoryEntry(
                            ldapDomainName, uid, passwordLdap,
                            AuthenticationTypes.None);

                        try
                        {
                            var connected = root.NativeObject;
                             return "LDAP Login SUCCESSFUL";
                            //isValid = true;
                            // no exception, login successful
                        }
                        catch (Exception ex)
                        {
                            // exception thrown, login failed
                            return "LDAP Login FAILED";
                        }
answered on Stack Overflow Apr 28, 2018 by Imtiaz Rifat

User contributions licensed under CC BY-SA 3.0