【问题标题】:How to remove orphaned accounts on folder permission Programmatically whith c#如何使用 c# 以编程方式删除文件夹权限上的孤立帐户
【发布时间】:2015-02-20 11:26:06
【问题描述】:

我有一个小程序可以从共享文件夹中删除帐户权限。但是在某些文件夹的安全选项卡上,有这样的帐户“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”类型的未处理异常

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

【问题讨论】:

    标签: c# file-security


    【解决方案1】:

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

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

    稍后您将帐户提供给

    new FileSystemAccessRule(account, ...
    

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

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

    我稍微改了一下代码:

    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 的条目。

    【讨论】:

    • 首先感谢您的回答,其次您理解正确。我正在获取文件夹对列表视图项目的访问权限,例如在文件夹属性内的安全选项卡中。并单击它们以删除此 SID。这就是我关心 System.Security.Principal.NTAccount 的原因。
    猜你喜欢
    • 2020-06-16
    • 1970-01-01
    • 2014-07-03
    • 2011-02-12
    • 1970-01-01
    • 2014-01-24
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多