blob: a9ae5aec9b4b25d3c91a7acf973d85c7a5f2b7c1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
}
}
|