我正在尝试制作一个Visual Studio程序,使您可以将用户添加到Active Directory。每当添加用户时,都会出现错误“指定了无效的DN语法”。我不知道此错误的确切含义,而Googling得知我这可能是很多事情。以下是相关代码:

            string UserName = txt_UserName.Text;
            string Department = ddl_Department.SelectedItem.ToString(); //The chosen OU
            string LDAPstring = "LDAP://OU=" + Department + ",DC=group0, DC=han";
            DirectoryEntry dirEntry = new DirectoryEntry(LDAPstring);

            //Make a USER Object
            DirectoryEntry newUser = dirEntry.Children.Add(UserName, "user");
            newUser.CommitChanges(); //User is made
            newUser.Properties["userprincipalname"].Add(txt_UserName.Text + "@group0.han");
            newUser.Properties["samaccountname"].Value = txt_UserName.Text;
            newUser.CommitChanges(); //User is given properties

最佳答案

我认为如果更改此行,您应该很好:

DirectoryEntry newUser = dirEntry.Children.Add(UserName, "user");

对此:

DirectoryEntry newUser = dirEntry.Children.Add("CN=" + UserName, "user");

另外,userPrincipalName不是多值的。就像设置sAMAccountName值一样,只需对其设置值即可。

10-05 23:18