gpt4 book ai didi

c# - 创建用户后将用户添加到 AD 安全组失败

转载 作者:行者123 更新时间:2023-11-30 22:46:59 24 4
gpt4 key购买 nike

我正在使用 WCF 服务向我们的帮助台员工公开某些 Active Directory 管理功能,而不向他们提供直接操作 AD 所需的组成员身份。将用户添加到组和从组中删除用户对现有用户来说非常有效,但每次我创建新用户时,它都会返回这个有趣的代码:

The server is unwilling to process the request. (Exception from HRESULT: 0x80072035)

我用来将用户添加到组的代码是

    public bool AddGroupToUser(string userDn, string groupDn)
{
try
{
DirectoryEntry groupEntry = LdapTools.GetDirectoryEntry(groupDn);
groupEntry.Properties["member"].Add(userDn);
groupEntry.CommitChanges();
groupEntry.Close();
return true;
}
catch (DirectoryServicesCOMException)
{
return false;
}
}

我读到的关于这个主题的所有内容都相当模糊,我似乎无法找出为什么会触发异常。有什么想法吗?

更新

这是我用来在 AD 中创建用户的代码:

        try
{
DirectoryEntry container = GetDirectoryEntry(storageOu);
DirectoryEntry newUser = container.Children.Add("CN=" + employee.FullName, "user");
newUser.Properties["sAMAccountName"].Value = employee.Username;
newUser.Properties["displayName"].Value = employee.FullName;
newUser.Properties["givenName"].Value = employee.FirstName;
newUser.Properties["sn"].Value = employee.LastName;
newUser.Properties["department"].Value = departmentName;
newUser.Properties["userPrincipalName"].Value = employee.Username + "@APEX.Local";
newUser.CommitChanges();

newUser.Invoke("SetPassword", new object[] { employee.Password });
newUser.CommitChanges();

AdsUserFlags userSettings = AdsUserFlags.NormalAccount;
newUser.Properties["userAccountControl"].Value = userSettings;
newUser.CommitChanges();

ldapPath = newUser.Path;

newUser.Close();
container.Close();
}
catch (DirectoryServicesCOMException e)
{
// Something went wrong... what???
}
catch (Exception e)
{
// Something else went wrong
}

新用户可以登录,并且可以使用标准的 MS 工具进行操作。

最佳答案

显然,除非我在这里错过了重要的一步,否则问题就是时间。在尝试将组添加到新用户之前强制系统休眠 8 秒时,该过程有效。如果我在 8 秒标记之前执行此操作,它就会失败。

除非有人能为我提供更好的解决方案,否则我将此答案标记为正确。

关于c# - 创建用户后将用户添加到 AD 安全组失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2404177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com