代码之家  ›  专栏  ›  技术社区  ›  thaBadDawg

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

  •  2
  • thaBadDawg  · 技术社区  · 15 年前

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

    4 回复  |  直到 15 年前
        1
  •  2
  •   thaBadDawg    15 年前

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

    除非有人能给我提供更好的解决方案,否则我会把这个答案标为正确的。

        2
  •  1
  •   Michael D. Irizarry    15 年前

    尝试:

     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;
    
            }
    

    参考文献:
    http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/activedirectoryoperations11132009113015AM/activedirectoryoperations.aspx

        3
  •  0
  •   Mlh    15 年前

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

        4
  •  0
  •   Mlh    14 年前

    Active Directory复制问题可能会导致时间问题。

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

    所以,一位系统工程师告诉我们,在活动目录上的复制操作可能是问题的根源,这是真的。

    为了解决这个问题,程序尝试用1秒的睡眠时间做10次。到目前为止没有发生任何问题。所以作为一个解决方案,我建议你检查一下广告中是否存在复制方法。

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

    推荐文章