【发布时间】:2019-07-08 04:37:42
【问题描述】:
我在使用 ACL 和所有者创建文件夹时遇到了很多痛苦。
任务:
创建一个只能由一个用户(甚至管理员)访问的文件夹。
(当前)解决方案:
以管理员身份运行:
// path is the directory, "target" the parent directory
String path = Path.Combine(target, "Data");
DirectorySecurity ds = Directory.GetAccessControl(target);
// up is the "UserPrincipal"
ds.AddAccessRule(new FileSystemAccessRule(up.Sid, FileSystemRights.CreateDirectories, AccessControlType.Allow));
Directory.SetAccessControl(target, ds);
// safeTokenHandle_SecureUser is the token of the already logged in User stored in "up"
using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(safeTokenHandle_SecureUser.DangerousGetHandle()))
{
ds = new DirectorySecurity();
// Set owner only works impersonated
ds.SetOwner(up.Sid);
// Inherited needs impersonation
ds.AddAccessRule(new FileSystemAccessRule(up.Sid, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
// Add Backupgroup
ds.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier("S-1-5-32-551"), FileSystemRights.Read, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
DirectoryInfo directory = Directory.CreateDirectory(path, ds);
}
问题:
在“C:\temp\Sec53”中创建的路径(jea,数一下我的尝试次数……但多次尝试是我在同一个目录中……)
c:\temp\Sec53>whoami
pc-XXX\YYYuser93
c:\temp\Sec53>dir /q (1)
Datenträger in Laufwerk C: ist Windows
Volumeseriennummer: ...
Verzeichnis von c:\temp\Sec53
13.02.2019 13:13 <DIR> VORDEFINIERT\Administra. (2)
13.02.2019 13:13 <DIR> AAA\BBB ..
13.02.2019 13:13 <DIR> XXX\YYYUser93 Data
(TRIM)
c:\temp\Sec53>cacls *
c:\temp\Sec53\Data VORDEFINIERT\Sicherungs-Operatoren:(OI)(IO)(Beschränkter Zugriff:) (3)
READ_CONTROL
SYNCHRONIZE
FILE_GENERIC_READ
FILE_READ_DATA
FILE_READ_EA
FILE_READ_ATTRIBUTES
XXX\YYYUser93:(OI)(IO)F
(TRIM)
c:\temp\Sec53>cd Data
Zugriff verweigert (4)
- “Dir /q”显示所有者
- 用户是:预定义/管理员
- 用户是:预定义/备份操作员组
- 翻译:访问被拒绝
你可以看到,我用这个用户登录了 CMD。该目录存在且所有者是用户。此用户的权限设置为完全。但是我仍然无法在这个目录中进行更改。
怎么了?怎么回事?我能做什么?
【问题讨论】:
标签: c# .net acl windows-security