【问题标题】:Remove execute permission from uploaded folders and files [closed]从上传的文件夹和文件中删除执行权限[关闭]
【发布时间】:2019-03-24 10:17:47
【问题描述】:

我正在研究文件和文件夹上传系统,我想为其添加一些安全性。

我关注了这个Article,上面的安全点6号说:

6.严格控制权限

任何上传的文件都将归网络服务器所有。但它只需要 读/写权限,而不是执行权限。文件完成后 已下载,您可以应用额外的限制,如果这是 合适的。有时删除执行可能会有所帮助 来自目录的权限以防止服务器枚举 文件。

如何使用 C# 应用它

【问题讨论】:

    标签: c# .net system.io.file


    【解决方案1】:

    如果我对您的理解正确,您希望将文件上传到远程服务器,然后将文件更改为只读。这是一种选择。首先获取一个文件对象。之后,您可以设置访问控制,然后提供您要提供的访问权限。

    可能是这样的:

    using System.IO;
    using System.Security.AccessControl;
    
    private void SetFileAccess(string path)
        {
            var fileSecurity = new FileSecurity();
            var readRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.ReadData, AccessControlType.Allow);
            var writeRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.WriteData, AccessControlType.Allow);
            var noExecRule = new FileSystemAccessRule("identityOfUser", FileSystemRights.ExecuteFile, AccessControlType.Deny);
            fileSecurity.AddAccessRule(readRule);
            fileSecurity.AddAccessRule(writeRule);
            fileSecurity.AddAccessRule(noExecRule);
            File.SetAccessControl(path, fileSecurity);
        }
    

    MSDN Link to File

    MSDN Link to SetAccessControl Method

    MSDN Link to File System Rights

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2015-11-14
      • 2020-08-07
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多