Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建用户后,将用户添加到AD安全组失败_C#_Active Directory_Directoryservices - Fatal编程技术网

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

C# 创建用户后,将用户添加到AD安全组失败,c#,active-directory,directoryservices,C#,Active Directory,Directoryservices,我正在使用WCF服务向我们的帮助台员工公开某些Active Directory管理功能,而不授予他们直接操纵AD所需的组成员资格。向组中添加用户和从组中删除用户对现有用户来说就像一个冠军,但每次我创建一个新用户时,它都会抛出以下有趣的代码: The server is unwilling to process the request. (Exception from HRESULT: 0x80072035) 我用于将用户添加到组的代码是 public bool AddGroupToUs

我正在使用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工具进行操作。

尝试:

 public bool  AddUserToGroup(string userName, string groupName)

        {

            bool done = false;

            GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName);

            if (group == null)

            {

                group = new GroupPrincipal(context, groupName);

            }

            UserPrincipal user = UserPrincipal.FindByIdentity(context, userName);

            if (user != null & group != null)

            {

                group.Members.Add(user);

                group.Save();

                done = (user.IsMemberOf(group));

            }

            return done;

        }
参考资料:

如中所述,您能告诉我们您设置了新创建用户的密码吗?在参考资料中,它说在对用户进行任何操作之前,您应该设置用户的密码。

显然,除非我在这里遗漏了一个重要步骤,否则问题在于时间。在尝试向新用户添加组之前,强制系统睡眠8秒时,该过程有效。如果我早于8秒,它就会失败


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

时间问题可能由Active Directory复制问题引起

有一次,我给了我的客户一个产品,该产品通过Active Directory创建用户,Active Directory中有超过10000条记录,并在SharePoint表单上提供了给定的信息,然后程序将用户添加到SharePoint组。问题是,SharePoint抛出了一个关于新创建的用户的错误,它说该用户在AD中不存在

因此,我们的一位系统工程师告诉我们Active Directory上的复制操作可能是问题的根源,这是真的

对于解决方案,程序尝试在1秒睡眠的情况下执行10次该任务。到目前为止没有发生任何问题。因此,作为一个解决方案,我建议您查看广告中是否存在复制方法


附言:当我向客户的系统工程师询问有关复制操作的问题时,他们都拒绝接受AD复制操作的存在,并告诉我该程序存在问题。他们相信我,当我们从一台计算机在AD中创建一个新用户时,我们在另一台计算机上有5秒钟看不到该用户。

您也可以在我添加到问题中的代码更新中查看已接受的解决方案,我在其中添加了我的用户创建代码。除了我的用户的userAccountControl属性也有“Password not expire”值外,我在我的公司中使用了相同的代码。(ActiveDs.ADS\u USER\u FLAG.ADS\u UF\u don\u EXPIRE\u PASSWD)您也可以尝试设置此值吗?我不认为DirectoryEntry原语保证在内部使用相同的AD绑定,因此您完全有可能在不同的域控制器上操作组,而不是将用户添加到其中的域控制器(AD使用多主机复制模型)。复制完成所需的时间将取决于域控制器的数量,该设置决定在通知对等方更改之前等待多长时间以及AD事务量。在一个大型域中,我看到AD需要5分钟才能完全复制一个SPN,所以请注意8秒钟。我已经围绕DirectoryEntry编写了一个包装器,旨在解决不知道我连接到哪个域控制器的问题。我现在每次都连接到同一个DC,但在8秒的时间段过去之前,它仍然无法识别后续调用中的DirectoryEntry。幸运的是,至少现在,我只需要担心两个DC。如果我们开始在每个位置添加DC,老天保佑我。您知道一种方法来确定SPN何时被完全复制吗?