3

I'm trying to create a user with textboxes that have to be filled in. The properties, username and password are correct yet I get a "There is no such object on the server" error.

private void btn_AddStudent_Click(object sender, EventArgs e)
    {
        try
        {
            // Username and password.
            string UserName = UsernameGenerate(8);
            string Password = PasswordGenerate(8);

            // OU path.
            string ouString = "OU = " + cmb_Study.Text;
            string LDAPstring = "LDAP://" + "OU = Studies, " + ouString + ", DC=DR, DC=GUI";
            DirectoryEntry dirEntry = new DirectoryEntry(LDAPstring);

            // Create user.
            string userString = "CN = " + UserName;
            DirectoryEntry newUser = dirEntry.Children.Add(userString, "user");
            newUser.CommitChanges();

            newUser.Properties["userprincipalname"].Add(UserName + "@DR.GUI");
            newUser.Properties["samaccountname"].Value = UserName;
            newUser.Invoke("setPassword", new object[] {Password});
            //Rest of the properties

            newUser.Close();
            dirEntry.Close();
            newUser.Dispose();
            dirEntry.Dispose();
            MessageBox.Show("User has been succesfully added");
        }
        catch (Exception E)
        {
            MessageBox.Show("Creating user has failed. " + E.Message);
        }
    }

I have been trying to search for a solution for quite a while now but I'm still clueless where the problem is. Did I make a mistake in the OU path? Which for example would be OU= Studies/ISM or Studies/WEBDEV. Or did I make any mistakes in my LDAP?

I'm fairly new to C# and programming in overal, I'm looking for a simple solution if possible.

  • 1
    For user management I would suggest to use [System.DirectoryServices.AccountManagement](https://www.codeproject.com/Articles/38344/Using-System-DirectoryServices-AccountManagement) – Renatas M. Oct 23 '17 at 13:06

0 Answers0