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 abc.def.com.
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("13.18.12.16", "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();
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:
LDAP://cn=Users,dc=abc,dc=def,dc=com
Also consider what Hall72215 mentioned in his answer. Use the whole LDAP path directly in the constructor of the DirectoryEntry
class.
Why are you giving one path in the constructor (13.18.12.16
), 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://13.18.12.16/OU=Users,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 13.18.12.16
?
User contributions licensed under CC BY-SA 3.0