如何将新组应用到 Microsoft Active Directory 中 OU 中的用户

How do I apply a new group to a user in an OU in Microsoft Active Directory

我的系统让用户登录 register/renew 他们的帐户。在后端我们已经设置了一个组并检查他们是否已经在那里。我们还更改了我们的系统,如果用户正在续订(已经在一个 OU 中),则将他们移动到一个新的 OU。

我现在要做的是在用户续订或注册时为他们申请一个新组。导致用户成为 2 个组的成员。

DirectoryEntry instructorRoot = new DirectoryEntry(LDAP_OU_DIR); //root binding
instructorRoot.AuthenticationType = AuthenticationTypes.Signing | AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.FastBind;

DirectoryEntry instructor = new DirectoryEntry(LDAPRoot); //default value
instructor.AuthenticationType = instructorRoot.AuthenticationType;

/*Here is where im trying to look for the TestGroup group*/
DirectoryEntry instructorGroup = instructorRoot.Children.Find("CN="+TestGroup, "group");
instructorGroup.AuthenticationType = instructorRoot.AuthenticationType;

instructor = instructorRoot.Children.Add("CN=" + hfUser.Value, "user");
instructor.CommitChanges();

instructor.Properties["userPrincipalName"].Value = hfUser.Value + "@" + LDAPRoot;
instructor.Properties["sAMAccountName"].Value = hfUser.Value; //login name
instructor.CommitChanges();

/*Here is where im trying to add the InstTest group to the user*/ 
instructorGroup.Properties["member"].Add(instructor.Properties["distinguishedName"].Value); //add to instructors group
instructorGroup.CommitChanges(); //commit changes so that we can set primary group next
instructorGroup.Close();//close

instructor.Properties["PrimaryGroupID"].Value = 109929; //set primarygroup to instructors
instructor.CommitChanges();
private void AddMemberToGroup(string bindString, string newMember)
{
    try
    {
        DirectoryEntry ent = new DirectoryEntry(bindString);
        ent.Properties["member"].Add(newMember);
        ent.CommitChanges();
    }
    catch (Exception e)
    {
        // do error catching stuff here
        return;
    }
}

其中 bindString 是包含您要将用户添加到的新组的完整目录的字符串。

newMember 是从用户对象的 .Properties["distinguishedName"].Value.ToString() 方法中获取的字符串。