启用 AD 帐户时出现重复条目​​错误

发布于 2024-11-15 02:53:00 字数 903 浏览 1 评论 0原文

我试图以编程方式将用户添加到 Active Directory,但总是遇到错误 - 无论我在启用帐户后创建什么用户,都会引发“重复条目”错误。这是我正在使用的代码:

DirectoryEntry NewUser = AD.Children.Add("CN=" + username, "User");
NewUser.CommitChanges();

//Add user information
NewUser.Invoke("SetPassword", password);
NewUser.Properties["givenName"].Value = FirstName;
NewUser.Properties["sn"].Value = LastName;
NewUser.Properties["mail"].Value = email;
NewUser.Properties["userPrincipalName"].Value = username + @"domainname";
NewUser.Properties["userAccountControl"].Add(0x200);//enable account
NewUser.CommitChanges();

当我注释掉更改 userAccountControl 的行时,一切正常。我什至尝试创建一个新条目并使用以下代码以这种方式修改它:

DirectoryEntry editUser = getUserEntry(username);
editUser.Properties["userAccountControl"].Add(0x200);//enable account
editUser.CommitChanges();

但这仍然会引发相同的错误。 getUserEntry 只是从 AD 中获取给定用户名的目录条目。谁能看出在这种情况下会发生重复输入错误的原因吗?

I am trying to add users to Active Directory programmatically and I keep running into an error - no matter what user I create as soon as I enable the account a "Duplicate Entry " error is thrown. This is the code that I am using:

DirectoryEntry NewUser = AD.Children.Add("CN=" + username, "User");
NewUser.CommitChanges();

//Add user information
NewUser.Invoke("SetPassword", password);
NewUser.Properties["givenName"].Value = FirstName;
NewUser.Properties["sn"].Value = LastName;
NewUser.Properties["mail"].Value = email;
NewUser.Properties["userPrincipalName"].Value = username + @"domainname";
NewUser.Properties["userAccountControl"].Add(0x200);//enable account
NewUser.CommitChanges();

When I comment out the line that changes the userAccountControl, everything works fine. I even tried to create a new entry and modify it that way using this code:

DirectoryEntry editUser = getUserEntry(username);
editUser.Properties["userAccountControl"].Add(0x200);//enable account
editUser.CommitChanges();

but that still throws the same error. getUserEntry just grabs the directory entry from AD given the username. Can anyone see a reason why a duplicate entry error would occur in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2024-11-22 02:53:00

您可以尝试替换为:

DirectoryEntry editUser = getUserEntry(username);
editUser.Properties["userAccountControl"][0] = (0x200);//enable account 
editUser.CommitChanges(); 

您可能知道某些属性可以是多值的,userAccountControl不能,但是在您的代码中这就是您尝试做的,我意味着对其进行多值化。在我的代码中,我只是分配一个新值(它将在 vue 的纯 LDAP 点上进行替换)。

Can you try to replace by :

DirectoryEntry editUser = getUserEntry(username);
editUser.Properties["userAccountControl"][0] = (0x200);//enable account 
editUser.CommitChanges(); 

You probably know that some attributes can be multi-valued, userAccountControl can't, but in your code that's what you try to do, I mean to multi-value it. In my code I just assign a new value (it's going to play a replace on the pure LDAP point of vue).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文