Search code examples
c#active-directorywindows-server-2008windows-server-2008-r2windows-security

Reading / Writing security properties to objects in Active Directory (the same way Delegation of rights work) C#


I'm looking for a way to read and set security permissions on an object (OU or users/computers) in Active Directory on Windows Server 2008+. The same way that Delegation by using Active Directory Wizard does it? I would like to be able to choose OU and assign group to it with Reset Password permissions or with ability to create / manage users?

How can I achieve that?


Solution

  • So here is a simple example that allow the domain user 'user1' to reset password for users presents in OU 'ForUser1'

    /* Connection to Active Directory
     */
    DirectoryEntry workingOU = new DirectoryEntry();
    workingOU.Options.SecurityMasks = SecurityMasks.Owner | SecurityMasks.Group | SecurityMasks.Dacl | SecurityMasks.Sacl;
    workingOU.Path = "LDAP://WM2008R2ENT:389/ou=ForUser1,dc=dom,dc=fr";
    
    /* Retreive Obect security
     */
    ActiveDirectorySecurity adsOUSec = workingOU.ObjectSecurity;
    
    /* Ellaborate the user to delegate
     */
    NTAccount ntaToDelegate = new NTAccount("dom", "user1");
    SecurityIdentifier sidToDelegate = (SecurityIdentifier)ntaToDelegate.Translate (typeof(SecurityIdentifier));
    
    /* Specils Guids
     */
    Guid UserForceChangePassword = new Guid("00299570-246d-11d0-a768-00aa006e0529");
    Guid userSchemaGuid = new Guid("BF967ABA-0DE6-11D0-A285-00AA003049E2");
    Guid pwdLastSetSchemaGuid = new Guid("bf967a0a-0de6-11d0-a285-00aa003049e2");
    
    /* Ellaborate ACEs
     */
    ExtendedRightAccessRule erarResetPwd = new ExtendedRightAccessRule(ntaToDelegate, AccessControlType.Allow, UserForceChangePassword, ActiveDirectorySecurityInheritance.Descendents, userSchemaGuid);
    PropertyAccessRule parPwdLastSetW = new PropertyAccessRule(ntaToDelegate, AccessControlType.Allow, PropertyAccess.Write, pwdLastSetSchemaGuid, ActiveDirectorySecurityInheritance.Descendents, userSchemaGuid);
    PropertyAccessRule parPwdLastSetR = new PropertyAccessRule(ntaToDelegate, AccessControlType.Allow, PropertyAccess.Read, pwdLastSetSchemaGuid, ActiveDirectorySecurityInheritance.Descendents, userSchemaGuid);
    adsOUSec.AddAccessRule(erarResetPwd);
    adsOUSec.AddAccessRule(parPwdLastSetW);
    adsOUSec.AddAccessRule(parPwdLastSetR);
    
    workingOU.CommitChanges();
    

    After that you need :

    a place to find ExtendedRightAccessRule.

    a place to find Active-Directory schema attributes and classes informations.