簡體   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