指定了无效的DN语法-Visual Studio错误

本文关键字:语法 -Visual Studio 错误 DN 无效 | 更新日期: 2023-09-27 18:20:16

我正在尝试制作一个Visual Studio程序,用于将用户添加到Active Directory。每当我添加用户时,我都会收到错误"指定了无效的DN语法"。我不知道这个错误到底意味着什么,谷歌搜索告诉我,这可能是很多事情。以下是相关代码:

            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

指定了无效的DN语法-Visual Studio错误

我认为如果你更改这一行,你应该很好:

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

到此:

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

此外,userPrincipalName不是多值的。只需在上面设置值,就像设置sAMAccountName值一样。