ObjectSecurity is an abstract base class defined in the .NET Framework within the System.Security.AccessControl namespace. It encapsulates the access control and audit security information for securable objects, such as files, directories, registry keys, and other system resources. By providing a common set of properties and methods, ObjectSecurity enables developers to query, modify, and persist security descriptors in a consistent manner across different object types.
Core Characteristics
- Namespace:
System.Security.AccessControl - Assembly:
mscorlib.dll(orSystem.Security.AccessControl.dllin later .NET versions) - Accessibility: Public, abstract; cannot be instantiated directly.
- Purpose: Serves as the foundational class for more specific security classes that represent particular object types (e.g.,
FileSecurity,DirectorySecurity,RegistrySecurity,MutexSecurity).
Primary Members
| Member Type | Signature | Description |
|---|---|---|
| Property | AccessRuleFactory (protected) |
Creates access rule objects appropriate for the derived class. |
| Property | AuditRuleFactory (protected) |
Creates audit rule objects appropriate for the derived class. |
| Method | GetAccessControl() (abstract) |
Retrieves the current access control settings for the underlying object. |
| Method | SetAccessControl(ObjectSecurity securityDescriptor) (abstract) |
Applies a modified access control descriptor to the underlying object. |
| Method | AddAccessRule(AccessRule rule) |
Adds a new access rule to the security descriptor. |
| Method | RemoveAccessRule(AccessRule rule) |
Removes a specific access rule, if it exists. |
| Method | ResetAccessRule(AccessRule rule) |
Replaces all existing rules for a specific identity with the supplied rule. |
| Method | SetAccessRule(AccessRule rule) |
Adds the rule if it does not exist; otherwise, updates the existing rule. |
| Method | GetAccessRules(bool includeExplicit, bool includeInherited, Type targetType) |
Returns a collection of access rules, optionally filtering by inheritance and target identity type. |
| Method | GetAuditRules(bool includeExplicit, bool includeInherited, Type targetType) |
Returns a collection of audit rules, with similar filtering options. |
| Property | AccessRightType (protected) |
Indicates the enumeration type used for access rights in derived classes. |
| Property | AuditRuleType (protected) |
Indicates the enumeration type used for audit rules in derived classes. |
Derived Classes
ObjectSecurity is extended by several concrete classes that implement its abstract members for specific resource types:
- FileSecurity – Manages access control for files.
- DirectorySecurity – Manages access control for directories.
- RegistrySecurity – Manages access control for Windows registry keys.
- MutexSecurity – Manages access control for mutex objects.
- EventWaitHandleSecurity – Manages access control for event wait handles.
Each derived class provides constructors that accept a security descriptor in binary or SDDL (Security Descriptor Definition Language) form, as well as overloads that enable creation from existing system objects.
Typical Usage Pattern
using System.IO;
using System.Security.AccessControl;
// Obtain a FileSecurity object for an existing file.
FileInfo fileInfo = new FileInfo(@"C:\example.txt");
FileSecurity security = fileInfo.GetAccessControl();
// Create a new access rule granting read permission to a user.
FileSystemAccessRule rule = new FileSystemAccessRule(
@"DOMAIN\User",
FileSystemRights.Read,
AccessControlType.Allow);
// Add the rule to the security descriptor.
security.AddAccessRule(rule);
// Persist the changes back to the file.
fileInfo.SetAccessControl(security);
In this pattern, FileSecurity inherits from ObjectSecurity, leveraging its generic handling of access and audit rules while providing file‑specific enumerations (FileSystemRights).
Security Context
ObjectSecurity operates on Security Descriptors, which consist of:
- Owner – The identity that owns the object.
- Group – The primary group associated with the object.
- DACL (Discretionary Access Control List) – Defines who is granted or denied access.
- SACL (System Access Control List) – Defines audit logging for access attempts.
Manipulating these descriptors requires appropriate privileges (e.g., SeSecurityPrivilege for SACL changes) and may be subject to operating‑system security policies.
Platform Support
- .NET Framework: Available from version 2.0 onwards.
- .NET Core / .NET 5+: Included in the
System.Security.AccessControlpackage. - Operating Systems: Primarily Windows; limited functionality on Unix‑like systems due to differing security models.
References
- Microsoft Docs –
System.Security.AccessControl.ObjectSecurityclass reference. - Windows Security Model documentation (covers security descriptors, DACLs, SACLs).