0

我有一个小程序可以从共享文件夹中删除帐户权限。但是在某些文件夹的安全选项卡上,有这样的帐户“S-1-5-21-2008445439-890656017-1691616715-1589748”。我有权登录该服务器并手动删除,但由于下面的错误,我无法执行我的代码。如何删除这些帐户。谢谢。

private void button2_Click(object sender, EventArgs e)
    {
        var security = Directory.GetAccessControl(txtBoxPath.Text);
        var rules = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));

        foreach (FileSystemAccessRule rule in rules)
        {
                if (rule.IdentityReference.Value == listView1.SelectedItems[0].Text)
                {
                    string name = rule.IdentityReference.Value;
                    RemoveFileSecurity(txtBoxPath.Text, name,
                    FileSystemRights.FullControl |
                    FileSystemRights.Modify |
                    FileSystemRights.Read |
                    FileSystemRights.ReadAndExecute |
                    FileSystemRights.ReadPermissions |
                    FileSystemRights.Synchronize |
                    FileSystemRights.ListDirectory |
                    FileSystemRights.ChangePermissions |
                    FileSystemRights.Delete,
                    AccessControlType.Allow);
                    MessageBox.Show("OK");
                }
         }
    }

public static void RemoveFileSecurity(string fileName, string account,
        FileSystemRights rights, AccessControlType controlType)
    {
        // Get a FileSecurity object that represents the 
        // current security settings.
        FileSecurity fSecurity = File.GetAccessControl(fileName);
        // Remove the FileSystemAccessRule from the security settings.
        fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
            rights, controlType));
        // Set the new access settings.
        File.SetAccessControl(fileName, fSecurity);

    }

mscorlib.dll 中出现“System.Security.Principal.IdentityNotMappedException”类型的未处理异常

附加信息:部分或全部身份参考无法翻译。

4

1 回答 1

0

我检查了这段代码(如果重要的话,使用 .NET 4.0):IdentityReference 不会发生异常。

在 foreach 循环中读取条目是可以的,如果 ACE(访问控制条目)包含无法解析的受托者(用户或组),它会返回一个 SID(S-1-5-21-20084454... .) 作为价值。在这一点上这很好,并且框架代码可以在这里做到最好。

稍后您将帐户提供给

new FileSystemAccessRule(account, ...

此时会发生异常,因为account将被视为帐户 NAME 并且将进行 SID 查找的名称。并且由于“S-1-5...”不是有效的帐户名,构造函数会抛出异常。

但是:为什么要使用字符串作为RemoveFileSecurity方法的参数?

我稍微更改了代码:

foreach (FileSystemAccessRule rule in rules)
{
    if (rule.IdentityReference.Value == listView1.SelectedItems[0].Text)
    {
        RemoveFileSecurity(path, rule);
        MessageBox.Show("OK");
    }
}



public static void RemoveFileSecurity(string fileName, FileSystemAccessRule rule)
{
    // Get a FileSecurity object that represents the 
    // current security settings.
    FileSecurity fSecurity = File.GetAccessControl(fileName);

    // Remove the FileSystemAccessRule from the security settings.
    fSecurity.RemoveAccessRule(rule);

    // Set the new access settings.
    File.SetAccessControl(fileName, fSecurity);

}

我希望我正确理解了您的问题。我假设您确实在文本框中输入了 SID,并希望删除带有 SID 的条目。

于 2015-02-20T13:00:05.627 回答