c# - 无法删除目录 ACE

标签 c# .net acl

我正在使用 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 时,更改才会生效。换句话说,您不能一次性删除访问权限的继承并修改它们。它必须分两个阶段完成,即:

  1. disec.SetAccessRuleProtection(true, true);
  2. m_diWork.SetAccessControl(disec);
  3. disec = m_diWork.GetAccessControl()
  4. disec.PurgeAccessRules(act);
  5. m_diWork.SetAccessControl(disec);

PurgeAccessRules 不通知有关删除访问权限的问题并不明显,也无济于事。

关于c# - 无法删除目录 ACE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514751/

相关文章:

c# - 如何在对象上进行交易

c# - 使用正则表达式 C# 将表情符号替换为推文中的单词

c# - TypedReference 的实际使用

php - CodeIgniter——ACL 的最佳实现

c# - 如何以编程方式修复非规范 ACL?

c# - WebView cookie Windows 8

c# - 在调用方法时使用 Moq 分配属性值

.net - 真实世界 strtod();用于 WINAPI 文本框

c# - 无法在服务器上激活二进制 http 绑定(bind)

javascript - 如何将passport-jwt与ACL(访问控制列表)模式结合起来