我正在努力克服以下情况.
给定存储在NTFS卷上的目录,其中:
(或者简而言之,所有管理员都被锁定在文件夹之外)
但!
(或者简而言之,我有权修复DACL /所有者)
我对以下代码应该没有问题:
WindowsIdentity privilegedUser = System.Security.Principal.WindowsIdentity.GetCurrent();
// I cannot use File.GetAccessControl() as I get access denied
// (working as intended! I have no access to read the ACL!)
// so I have to write a new ACL:
FileSecurity acl = new FileSecurity();
acl.SetOwner(admin.User);
acl.AddAccessRule(new FileSystemAccessRule(privilegedUser.User, FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl("c:\\path\\to\\broken", acl);
Run Code Online (Sandbox Code Playgroud)
但是,SetAccessControl调用会抛出UnauthorizedAccessException.当我改变它只调整所有者时,同样的事情发生.当我只尝试调整DACL时,同样的事情.
我通过在Process Explorer中检查生成的进程并验证Administrators组是否设置为"Owner"而不是"Disabled"来验证问题不是UAC.我应该拥有执行此操作所需的所有权限(备份操作员在面对管理员时应该是无关紧要的,但我将其添加进行测试) - 但它只是继续拒绝访问权限.
相关的technet文档:http://technet.microsoft.com/en-us/library/cc783530%28WS.10%29.aspx