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

时间:2015-11-02 13:50:39

标签: c# visual-studio active-directory

我正在尝试制作一个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

1 个答案:

答案 0 :(得分:1)

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

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

到此:

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

此外,userPrincipalName并非多值。只需在其上设置值,就像设置sAMAccountName值一样。