Connect to Active Directory using credential


I'm trying to connect to AD sever using C#. This is my first time playing with AD.Domain I need to connect to is

This is a ASP.NET web site and it gives this error. But I can log in to same domain using "ldp.exe" by using same credential. Anyone have idea?

[DirectoryServicesCOMException (0x8007052e): Logon failure: unknown user name or bad "password.
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +387825
   System.DirectoryServices.DirectoryEntry.Bind() +36
   System.DirectoryServices.DirectoryEntry.get_AdsObject() +31

This is my code

static System.DirectoryServices.DirectoryEntry createDirectoryEntry()
        System.DirectoryServices.DirectoryEntry ldapConnection = new System.DirectoryServices.DirectoryEntry("", "Administrator", "admin123");
        ldapConnection.Path = "LDAP://ou=Users,dc=abc,dc=def,dc=com";
        ldapConnection.AuthenticationType = System.DirectoryServices.AuthenticationTypes.Secure;
        return ldapConnection;

System.DirectoryServices.DirectoryEntry sgscAd = createDirectoryEntry();
System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(sgscAd);
search.Filter = "(cn=" + m_username + ")";

System.DirectoryServices.SearchResult result = search.FindOne();

2 Answers


The LDAP path to the users container is not correct. The users container is not an organizational unit but a simple container. So, you have to specify a different LDAP path.

The LDAP path to the users container in your case is:


Also consider what Hall72215 mentioned in his answer. Use the whole LDAP path directly in the constructor of the DirectoryEntry class.

answered on Stack Overflow Aug 30, 2013 by Hans • edited Aug 31, 2013 by Hans

Why are you giving one path in the constructor (, and another by setting the Path property? Have you tried giving all of the information in the constructor?

static DirectoryEntry createDirectoryEntry()
    string username = "Administrator";
    string password = "admin123";
    string path = "LDAP://,DC=abc,DC=def,DC=com";
    AuthenticationTypes authType = AuthenticationTypes.Secure | AuthenticationTypes.ServerBind;
    return new DirectoryEntry(path, username, password, authType);

Is Administrator a user in the domain of the domain controller at

answered on Stack Overflow Aug 31, 2013 by Sean Hall

User contributions licensed under CC BY-SA 3.0