开发者

How do I set the ManagedBy property on a GroupPrincipal

开发者 https://www.devze.com 2023-01-08 05:33 出处:网络
I\'m creating and updating Groups in Active Directory using the Gr开发者_开发百科oupPrincipal class in System.DirectoryServices.AccountManagement. When creating and updating, I also need to be able to

I'm creating and updating Groups in Active Directory using the Gr开发者_开发百科oupPrincipal class in System.DirectoryServices.AccountManagement. When creating and updating, I also need to be able to set the ManagedBy property that you are able to set in the Managed By tab in the groups properties in the AD management console.

Can it be done programatically?


You cannot do this directly, unfortunately - but you can get access to the underlying DirectoryEntry and do it there:

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


You could extend the GroupPrincipal class and provide a ManagedBy property using the ExtensionSet method.


Take a look at this page. This is one of the best tutorials on AD in c#.

Some code that should work(untested) :

    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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号