Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'd like to set below options for an AD (Active Directory, Windows Server 2008 R2) OU for a particular user group using C# (.NET 4.0).

1.List contents
2.Read all Properties
3.Write all Properties
4.Read Permissions
5.Modify permission
6.Change Password
7.Reset Password
8.Validated write to DNS host Name
9.Validated write to Service Principal Name


Below working for all properties i want above options only

What I have tried:

C#
using (DirectoryEntry computers = new DirectoryEntry(GrpAccpath, LDAPUser, LDAPPassword))
                {
                    computers.Options.SecurityMasks = System.DirectoryServices.SecurityMasks.Owner | System.DirectoryServices.SecurityMasks.Group |
                                                            System.DirectoryServices.SecurityMasks.Dacl | System.DirectoryServices.SecurityMasks.Sacl;

                    foreach (DirectoryEntry computer in computers.Children)
                    {
                        if (computer.Name == "CN=" + ComputerAccountID)
                        {
                            for (i = 0; i < strArry.Length; i++)
                            {
                                ActiveDirectorySecurity sdc = computer.ObjectSecurity;
                                //  NTAccount Account = new NTAccount("Everyone");
                                try
                                {


                                    NTAccount Account = new NTAccount("Domain Admins");

                                    SecurityIdentifier Sid = (SecurityIdentifier)Account.Translate(typeof(SecurityIdentifier));
                                   

                                    ActiveDirectoryAccessRule rule = new ActiveDirectoryAccessRule(Sid, ActiveDirectoryRights.ExtendedRight |
                                                                     ActiveDirectoryRights.GenericRead | ActiveDirectoryRights.CreateChild |
                                                                     ActiveDirectoryRights.GenericWrite | ActiveDirectoryRights.AccessSystemSecurity |
                                                                     ActiveDirectoryRights.Delete | ActiveDirectoryRights.DeleteChild |
                                                                     ActiveDirectoryRights.DeleteTree | ActiveDirectoryRights.GenericAll |
                                                                     ActiveDirectoryRights.GenericExecute | ActiveDirectoryRights.GenericRead |
                                                                     ActiveDirectoryRights.GenericWrite | ActiveDirectoryRights.ListChildren |
                                                                     ActiveDirectoryRights.ListObject | ActiveDirectoryRights.ReadControl |
                                                                     ActiveDirectoryRights.ReadProperty | ActiveDirectoryRights.Self |
                                                                     ActiveDirectoryRights.Synchronize | ActiveDirectoryRights.WriteDacl |
                                                                     ActiveDirectoryRights.WriteDacl | ActiveDirectoryRights.WriteOwner |
                                                                     ActiveDirectoryRights.WriteProperty, AccessControlType.Allow);

                                         AccessControlType.Allow);
                                    if (Enable == true)
                                        sdc.AddAccessRule(rule);
                                    else
                                        sdc.RemoveAccessRule(rule);

                                                           
                                    computer.CommitChanges();
                                }
                                catch (Exception Ex)
                                {
                                    Logger.LogInfo(" Group Not Found  : " + Ex.ToString() + strArry[i]);
                                }
                            }
                            break;
                        }
Posted
Updated 29-Aug-19 21:44pm
v2
Comments
phil.o 30-Aug-19 4:44am    
Why don't you just strip off the permissions which you do not want from the list this piece of code is providing?
Nethaji chennai 30-Aug-19 5:20am    
can you Explain . iam new ActiveDirectoryAccessRule
DaveAuld 30-Aug-19 5:35am    
He means, if the code you have shown above does ALL properties, then delete the parts of it that relate to the properties you don't want.
phil.o 30-Aug-19 6:29am    
ActiveDirectoryRights.ExtendedRight | ActiveDirectoryRights.GenericRead | ActiveDirectoryRights.CreateChild | ActiveDirectoryRights.GenericWrite | ActiveDirectoryRights.AccessSystemSecurity | ActiveDirectoryRights.Delete | ActiveDirectoryRights.DeleteChild | ActiveDirectoryRights.DeleteTree | ActiveDirectoryRights.GenericAll | ActiveDirectoryRights.GenericExecute | ActiveDirectoryRights.GenericRead | ActiveDirectoryRights.GenericWrite | ActiveDirectoryRights.ListChildren | ActiveDirectoryRights.ListObject | ActiveDirectoryRights.ReadControl | ActiveDirectoryRights.ReadProperty | ActiveDirectoryRights.Self | ActiveDirectoryRights.Synchronize | ActiveDirectoryRights.WriteDacl | ActiveDirectoryRights.WriteDacl | ActiveDirectoryRights.WriteOwner | ActiveDirectoryRights.WriteProperty
I mean, among all those rights above, just strip off those you do not want. Mathematically, once you have done that, will only remain those you want.
Nethaji chennai 3-Sep-19 0:31am    
Yes, but if add any access rule multiple permission(need and not need options) added in ad. i want exact 9 above . Others not need


Ex: ActiveDirectoryRights.ExtendedRight add

below permission added in AD


1.Change Password
2.Reset Password
3.Allowed to authenticate
4.Receieve etc
but i want only
1.Change Password
2.Reset Password

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900