Search code examples
c#active-directoryldapdirectoryentry

c# LDAP connection to domain outside forest


I am trying to write some code to write objects into Active Directory using c#. I have a test Domain Controller that is not a member of the domain or forest that my laptop writing the code is a member of. I have tried the following substitutions for username and server in all combinations but when I get to the line

adobject = myLdapConnection.Children.Add(value, type);

I get back the error

The username or password is incorrect.

Why am I getting this error if the username and password are correct?

testuser is a domain admin. I can verify that it is correct with a program like Softerra LDAP Browser that connects to the server fine and lets me create objects.

//string username = "CN=testuser,CN=Users,DC=ad,DC=domain,DC=com";
//string username = "testuser";
//string username = "AD\\testuser";

//string server="LDAP://10.10.10.10:389";
//string server="LDAP://10.10.10.10";

string server = "LDAP://10.10.10.10:389";
string basedn = "ou=testou,DC=ad,DC=domain,DC=com";
string username = "CN=testuser,CN=Users,DC=ad,DC=domain,DC=com";
string password="plaintextpassword";
DirectoryEntry myLdapConnection = new DirectoryEntry(server+"/"+basedn,username,password,AuthenticationTypes.Secure);
string value="ou=testsubou,ou=testou,DC=ad,DC=domain,DC=com";
string type="organizationalUnit";
DirectoryEntry adobject = myLdapConnection.Children.Add(
                                   value, type);
adobject.CommitChanges();

Solution

  • Username should be in "normal" format instead of "CN=testuser,CN=Users,DC=ad,DC=domain,DC=com";. Normal means DOMAIN\USER or [email protected]