我正在使用 C# 和 .NET 4 编写一个类库,它通过网络与共享服务器上的文件系统进行交互。我正在尝试调整文件夹的某些权限,我完全有能力添加 ACE,但我很难删除它们。
这是我目前的代码:
//get ACEs for the working folder.
DirectorySecurity disec = m_diWork.GetAccessControl();
//find out if the account we want to remove is inherited from a parent folder.
bool bIsAccountInherited = disec.GetAccessRules(false, true, typeof(NTAccount)).Cast<AuthorizationRule>().Any(ar => ar.IdentityReference.Value.Equals(act.Value, StringComparison.CurrentCultureIgnoreCase));
if (bIsAccountInherited)
{
//if so, remove inheritance of ACEs but preserve existing ones.
disec.SetAccessRuleProtection(true, true);
}
//remove all access to this account.
disec.PurgeAccessRules(act);
//commit changes to working folder.
m_diWork.SetAccessControl(disec);
变量 act
是 NTAccount 类型,指的是域用户。
代码运行没有异常或任何明显的问题,并且目标文件夹的权限已正确更改为非继承。但是,根本没有删除任何 ACE。
我尝试了几种不同的方法调用组合,也使用了 RemoveAccessRuleAll()
,但无济于事。我究竟做错了什么?谢谢。
最佳答案
问题在于您正试图删除不允许的继承访问权限。发生这种情况是因为调用 SetAccessRuleProtection(true, true) 还不够。仅当您随后调用 SetAccessControl 时,更改才会生效。换句话说,您不能一次性删除访问权限的继承并修改它们。它必须分两个阶段完成,即:
- disec.SetAccessRuleProtection(true, true);
- m_diWork.SetAccessControl(disec);
- disec = m_diWork.GetAccessControl()
- disec.PurgeAccessRules(act);
- m_diWork.SetAccessControl(disec);
PurgeAccessRules 不通知有关删除访问权限的问题并不明显,也无济于事。
关于c# - 无法删除目录 ACE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514751/