2010-07-20 34 views
6

我使用System.DirectoryServices.AccountManagement中的GroupPrincipal類創建和更新Active Directory中的組。創建和更新時,我還需要能夠設置ManagedBy屬性,您可以在AD管理控制檯的組屬性中的Managed By選項卡中設置該屬性。如何設置GroupPrincipal上的ManagedBy屬性

它可以通過編程方式完成嗎?

回答

8

你不能直接這樣做,遺憾的是 - 但你可以訪問底層DirectoryEntry並做到這一點有:這是不使用的OP請求System.DirectoryServices.AccountManagement命名空間

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"); 

UserPrincipal toBeModified = UserPrincipal.FindByIdentity("....."); 
UserPrincipal manager = UserPrincipal.FindByIdentity(ctx, "......"); 

DirectoryEntry de = toBeModified.GetUnderlyingObject() as DirectoryEntry; 

if (de != null) 
{ 
    de.Properties["managedBy"].Value = manager.DistinguishedName; 
    toBeModified.Save(); 
} 
+0

不應''tobeModified'類型'GroupPrincipal'? – 2016-11-28 20:08:42

+1

@PBMe_HikeIt:沒有,因爲'managedBy'屬性沒有在「GroupPrincipal」中「浮出水面」,您需要向「DirectoryEntry」降低「一級」並在那裏處理它 – 2016-11-28 20:26:17

0

看看this page。這是c#中AD最好的教程之一。

一些代碼,應該工作(未經測試):

string connectionPrefix = "LDAP://" + ouPath; 
    DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix); 
    DirectoryEntry newGroup = dirEntry.Children.Add 
     ("CN=" + groupName, "group"); 
    group.Properties["sAmAccountName"].Value = groupName; 
    newGroup.Properties["managedBy"].Value = managerDistinguishedName; 
    newGroup.CommitChanges(); 
    dirEntry.Close(); 
    newGroup.Close(); 
+0

... – 2010-07-20 11:23:27

+0

* http://www.codeproject.com/KB/system/everythingInAD.aspx#13*找不到 – Kiquenet 2016-06-15 06:37:09

1

你可以擴展GroupPrincipal類和使用方法ExtensionSet提供ManagedBy屬性。

+0

這就是我如何攻擊問題......雖然我無法正確使用ExtensionSet'正常工作 – 2010-08-19 20:25:59

+0

[代碼項目](https://www.codeproject.com/articles/118122/how-to-use-ad-attributes-not-represented-in-userpr)看起來像一個很好的解釋如何擴展GroupPrincipal上課使用inhe ritance – 2016-11-28 20:34:12