2013-07-09 32 views
0

我試圖添加一個用戶,將它們添加到一個組,然後使該組成爲用戶的主組。我一直在使用System.DirectoryServices.AccountManagement進行所有AD訪問。 我已經添加使用用戶:使用System.DirectoryServices.AccountManagement,使組成爲主組

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

是否有一個快速的方法來取得該組並使其主要組爲:

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(); 

我然後使用用戶添加到組用戶?一旦我創建了主要組,我將刪除默認的「域用戶」組。任何幫助表示讚賞。 -Cary

回答

0

這是由attribute primaryGroupID控制。它沒有被默認的UserPrincipal公開,所以你必須要麼make your own subclass that exposes it要麼使用更多的RAW底層System.DirectoryServices對象並設置屬性。

UPDATE:MSDN雜誌 2008和前面的文章中不再提供通過Web界面,您需要下載January 2008 magazine's chm file並找到文章「看它:管理目錄安全主體在.NET Framework 3.5「看到有關使子類文章)

的屬性值是RID組的,所以你需要從新組得到primaryGroupToken attribute並將其設置爲用戶primaryGroupID屬性。