繁体   English   中英

使用System.DirectoryServices.AccountManagement,将组设为主要组

[英]Using System.DirectoryServices.AccountManagement, Making a group the primary group

我试图添加一个用户,将他们添加到一个组,然后将该组作为用户的主要组。 我一直在使用System.DirectoryServices.AccountManagement进行所有AD访问。 我使用以下方式添加了用户:

principalContext = new PrincipalContext(ContextType.Domain, Globs.strDomain, userOU);
UserPrincipal userPrincipal = new UserPrincipal(principalContext);
userPrincipal.Surname = this.textBox_LastName.Text;
userPrincipal.GivenName = this.textBox_FirstName.Text;
userPrincipal.SamAccountName = this.textBox_LogonName.Text;
userPrincipal.MiddleName = this.textBox_Initials.Text;
userPrincipal.DisplayName = label_DisplayName.Text;
userPrincipal.Description = this.comboBox_Description.Text;
userPrincipal.UserPrincipalName = this.textBox_LogonName.Text;
userPrincipal.SetPassword(defaultPassword);
userPrincipal.PasswordNeverExpires = true;
userPrincipal.Enabled = true;
userPrincipal.Save();

然后,我使用以下方法将用户添加到组中:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Globs.strDomain))
{
    GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, groupName);
    group.Members.Add(pc, IdentityType.UserPrincipalName, userId);
    group.Save();
}

有没有一种快速的方法可以将该组设为用户的主要组? 成为主要组后,我将删除默认的“域用户”组。 任何帮助表示赞赏。 -卡里

这由属性primaryGroupID控制。 默认的UserPrincipal不会公开它,因此您必须创建自己的子类来公开它,或者使用更多的RAW基础System.DirectoryServices对象并设置属性。

更新: MSDN杂志的2008年及更早版本的文章不再通过Web界面提供。您需要下载2008年1月杂志的chm文件,并找到文章“ 查找:在.NET Framework 3.5中管理目录安全性主体 ”以请参阅有关制作子类的文章)

该属性值是该组的RID,因此您需要从新组中获取primaryGroupToken属性 ,并将其设置为用户primaryGroupID属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM