【问题标题】:How do I apply a new group to a user in an OU in Microsoft Active Directory如何将新组应用于 Microsoft Active Directory 中 OU 中的用户
【发布时间】:2015-04-09 22:11:12
【问题描述】:

我的系统有用户登录以注册/更新他们的帐户。在后端,我们已经设置了一个组并检查它们是否已经存在。如果用户正在续订(已经在 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();

【问题讨论】:

  • 另外我想补充一点,每个用户都是一个组的“成员”,并且该组被设置为主要组。当每个用户通过网页注册时,我想让这些帐户成为我的测试组 AInstTest(以编程方式)的“成员”。

标签: c# active-directory active-directory-group


【解决方案1】:
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() 方法获得的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多