【问题标题】:C# .Net FileSystemRights Deny Deletion permission and add later onC# .Net FileSystemRights 拒绝删除权限并稍后添加
【发布时间】:2020-02-07 21:13:40
【问题描述】:

我的程序生成一个文件。这个文件应该受到保护,这样用户就不会意外删除它。 因此,它需要以某种方式加以保护。

由于文件应该受到保护,而应用程序关闭 FileStream.Lock 不是此任务的合适解决方案。

我尝试在文件上拒绝 FileSystemRights.Delete,例如:

    fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                FileSystemRights.Delete, AccessControlType.Deny));

但这并不能阻止删除,为此我必须像这样更改它:

    fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                FileSystemRights.Delete | FileSystemRights.WriteAttributes, AccessControlType.Deny));

(用户可以打开文件属性并重新添加WriteAttribute权限,然后可以删除文件,这很好)

现在的问题是:该文件应该可以从应用程序中删除。但这样做:

    fSecurity.RemoveAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                FileSystemRights.Delete | FileSystemRights.WriteAttributes, AccessControlType.Deny));
    // or:
    fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                FileSystemRights.Delete | FileSystemRights.WriteAttributes, AccessControlType.Allow));

导致 UnauthorizedAccessException。所以我不能撤销我所做的。 这很奇怪,因为在文件资源管理器中它绝对可以这样做。


所以我的问题是 - 如何再次授予删除权限 - 或者:保护文件以防意外删除的最佳方法是什么



该文件已在 %appdata% 中,但由于用户可能删除了其他文件夹,因此绝对不能意外删除该文件

【问题讨论】:

  • 您必须删除之前添加的访问规则,而不是添加另一个。 ACL 是规则列表,操作系统通过这些规则查看是否有任何规则允许给定用户执行操作,或拒绝他们执行该操作。拒绝规则优先于允许规则,因此添加允许规则不会覆盖您之前添加的拒绝规则。
  • 文件包含什么?它是如何使用的?程序文件已经受到操作系统的保护——这就是你不能在Program Files 中写入任何内容的原因。如果应用程序使用适当的安装程序进行部署,操作系统将检测到更改
  • 应用程序数据应存储在ProgramData%APPDATA%。用户始终可以导航到该文件夹​​并修改文件,但这种可能性要小得多。

标签: c# .net file-security


【解决方案1】:

@canton7 谢谢!这非常有帮助

好的,经过反复试验,我得到了解决方案:

  1. 您必须将文件设置为只读。单独拒绝删除不起作用
  2. 您必须拒绝删除 + WriteAttributes - 如果您不这样做,可以在文件资源管理器中删除文件而无需请求权限。

  3. 再次解锁文件时:首先添加权限

    • 您必须删除您添加的拒绝规则
    • 您必须添加标志才能允许
    • 只做其中一项是行不通的
  4. 删除只读标志
        private static void LockFile(string _FullPath)
        {
            File.SetAttributes(_FullPath, File.GetAttributes(_FullPath) | FileAttributes.ReadOnly);

            FileSecurity fSecurity = File.GetAccessControl(_FullPath);

            fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                FileSystemRights.Delete | FileSystemRights.WriteAttributes, AccessControlType.Deny));

            File.SetAccessControl(_FullPath, fSecurity);
        }

        private static void UnLockFile(string _FullPath)
        {
            FileSecurity fSecurity = File.GetAccessControl(_FullPath);

            fSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                FileSystemRights.Delete | FileSystemRights.WriteAttributes, AccessControlType.Allow));

            fSecurity.RemoveAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                FileSystemRights.Delete | FileSystemRights.WriteAttributes, AccessControlType.Deny));

            File.SetAccessControl(_FullPath, fSecurity);

            File.SetAttributes(_FullPath, FileAttributes.Normal);
        }

【讨论】:

  • 这不要求运行应用程序的用户有Change permissions的权限吗?您是否必须以管理员身份运行才能正常工作?
  • 我使用此代码更改我的程序创建的文件的权限。所以程序有权限改变权限。它不能以管理员权限运行
猜你喜欢
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2014-03-09
  • 2016-06-13
  • 1970-01-01
  • 2019-03-08
  • 2021-10-21
相关资源
最近更新 更多