3

我正在尝试使用必须填写的文本框创建用户。属性、用户名和密码正确,但出现“服务器上没有此类对象”错误。

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);
        }
    }

我一直在尝试寻找解决方案很长一段时间,但我仍然不知道问题出在哪里。我在 OU 路径中犯了错误吗?例如,OU= Studies/ISM 或 Studies/WEBDEV。还是我在 LDAP 中犯了任何错误?

我对 C# 和总体编程相当陌生,如果可能的话,我正在寻找一个简单的解决方案。

4

0 回答 0