【发布时间】:2018-10-24 21:25:04
【问题描述】:
我正在尝试仅将权限分配给管理员并拒绝其他非管理员用户访问。下面是代码-
DirectoryInfo di = new DirectoryInfo(@"C:\C00");
DirectorySecurity dirSec = di.GetAccessControl();
dirSec.SetAccessRuleProtection(true, false);
SecurityIdentifier systemSid = new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null);
NTAccount systemAccount = (NTAccount)systemSid.Translate(typeof(NTAccount));
SecurityIdentifier adminSid = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
NTAccount adminAccount = (NTAccount)adminSid.Translate(typeof(NTAccount));
SecurityIdentifier userSid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
NTAccount userAccount = (NTAccount)userSid.Translate(typeof(NTAccount));
//access rule 1
dirSec.AddAccessRule(new FileSystemAccessRule(systemAccount, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
//access rule 2
dirSec.AddAccessRule(new FileSystemAccessRule(adminAccount, FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
//access rule 3
dirSec.AddAccessRule(new FileSystemAccessRule(userAccount, FileSystemRights.Read, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Deny));
di.SetAccessControl(dirSec);
return;
使用上面的代码,即使管理员也无法访问该文件夹,但如果没有注释中表示为“访问规则 3”的访问规则,它会按预期工作。任何人都可以向我解释为什么会这样正在发生吗?
【问题讨论】:
-
“拒绝”类型的优先级高于“允许”。因此,如果'George'是'user'和'administrator'并且用户有'deny',那么即使'George'也不能访问该文件夹。
-
是啊..有道理。那我怎样才能实现我想要的行为呢?
标签: c# .net windows permissions .net-4.5