diff options
Diffstat (limited to 'src/Agent.Windows/Security/UserOnlyDirectory.cs')
| -rw-r--r-- | src/Agent.Windows/Security/UserOnlyDirectory.cs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/Agent.Windows/Security/UserOnlyDirectory.cs b/src/Agent.Windows/Security/UserOnlyDirectory.cs new file mode 100644 index 0000000..a9ae5ae --- /dev/null +++ b/src/Agent.Windows/Security/UserOnlyDirectory.cs @@ -0,0 +1,20 @@ +using System.Security.AccessControl; +using System.Security.Principal; + +namespace Agent.Windows.Security; + +public static class UserOnlyDirectory +{ + public static void Ensure(string path) + { + Directory.CreateDirectory(path); + using WindowsIdentity identity = WindowsIdentity.GetCurrent(); + SecurityIdentifier user = identity.User ?? throw new InvalidOperationException("Current Windows identity has no user SID."); + var security = new DirectorySecurity(); + security.SetOwner(user); + security.SetAccessRuleProtection(isProtected: true, preserveInheritance: false); + security.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, + InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); + FileSystemAclExtensions.SetAccessControl(new DirectoryInfo(path), security); + } +} |
