C# Can not add user to Active Directory. It show error There is no such object on the server

Viewed 4402

I want to use C# add user to Active Directory from my computer. AD install in server 10.1.1.5 Username and password for login to AD is "Administrator", "Pass12345678". I create OU name Guest on AD.

This is my code to add user to AD.

private void Btn_ok_Click(object sender, EventArgs e)
        {
            UserInfo newUserInfo;
            newUserInfo.firstName = "TestName" ;
            newUserInfo.lastName = "TestLastName";
            newUserInfo.displayName = "TestName Lastname";

            newUserInfo.username = "TestName.t";
            newUserInfo.sAMAccountName = "TestName.t";

            DirectoryEntry adUserFolder = new DirectoryEntry("LDAP://10.1.1.5/CN=Users,OU=Guest,DC=myhome,DC=com", "Administrator", "Pass12345678");

/******** It show error this line **********/

            if(adUserFolder.SchemaEntry.Name == "container")
            {
                DirectoryEntry newUser = adUserFolder.Children.Add("CN=" + newUserInfo.username, "User");

                if (DirectoryEntry.Exists(newUser.Path))
                {
                    //Remove exist user
                    adUserFolder.Children.Remove(new DirectoryEntry(newUser.Path));

                 }

                newUser.Properties["sAMAccountName"].Value = newUserInfo.sAMAccountName;
                newUser.Properties["givenName"].Value = newUserInfo.firstName;
                newUser.Properties["sn"].Value = newUserInfo.lastName;
                newUser.Properties["displayName"].Value = newUserInfo.displayName;

                newUser.CommitChanges();
                newUser.Invoke("setpassword", "Test123456");

                newUser.CommitChanges();

            }
        }

when I click OK button it show error.

System.DirectoryServices.DirectoryServicesCOMException: 'There is no such object on the server. '

at line

if(adUserFolder.SchemaEntry.Name == "container")

How to fix it ?

2 Answers

For creating user objects in active directory I've always used UserPrincipals as opposed to DirectoryEntries:

public void create(string lanid, string new_password, string container)
    {
        using (UserPrincipal new_user = new UserPrincipal(new PrincipalContext(ContextType.Domain, this.domain_string, container)))

        {
            new_user.SamAccountName = lanid;
            new_user.SetPassword(new_password);
            new_user.Enabled = true;
            new_user.Save();
        }
    }

In that example "container" would be something like:

"OU=container,OU=container,OU=container,OU=container,DC=domain,DC=domain,DC=domain";

As for your original error with the DirectoryEntries I'd step through it and see if the object is actually set to anything:

The string:

"LDAP://10.1.1.5/CN=Users,OU=Guest,DC=myhome,DC=com"

Doesn't look right to me;

"LDAP://CN=10.1.1.5,CN=Users,OU=Guest,DC=myhome,DC=com"

May work...

The DirectoryEntry constructor will never throw an exception. It doesn't actually connect to AD until you start using it. Since that line is the first time you are using adUserFolder, that's when it first connects to AD.

The error means what it says: there is no object with a distinguishedName of CN=Users,OU=Guest,DC=myhome,DC=com.

Are you sure you have that right? It seems like you are looking for a container called Users inside of an OU called Guest. Is that what you are trying to do?

Is Users a container or an OU?

You can confirm the distinguishedName by using AD Users and Computers: navigate to the object you want -> right-click -> 'Properties' -> Attribute Editor tab and look at the distinguishedName attribute.

Related