检查C#.NET内核中的文件权限

检查C#.NET内核中的文件权限,c#,.net-core,file-permissions,.net-core-3.1,C#,.net Core,File Permissions,.net Core 3.1,我正在做一个项目,我必须对文件进行健全性检查。我需要确保当前系统用户对该文件具有读取权限,我首先尝试使用: var readPermission = new FileIOPermission(FileIOPermissionAccess.Read, filePath); try { readPermission.Demand(); } catch (SecurityException ex) { //handle the exception, which should be t

我正在做一个项目,我必须对文件进行健全性检查。我需要确保当前系统用户对该文件具有读取权限,我首先尝试使用:

var readPermission = new FileIOPermission(FileIOPermissionAccess.Read, filePath);

try
{
    readPermission.Demand();
}
catch (SecurityException ex)
{
    //handle the exception, which should be thrown if current user does NOT have the read permission
}
这不起作用,例如,没有抛出异常,因此我尝试这样做:

var readPermission = new FileIOPermission(FileIOPermissionAccess.Read, filePath);


if(! SecurityManager.IsGranted(readPermission))
{

    throw new SecurityException(
        String.Format("System user: {0} does not have read access to file {1}", User.Identity.Name, filePath)
        );
}
然而,SecurityManager api似乎大部分都被弃用,因此这似乎也是一条死胡同。是否有其他方法可以告诉用户对文件拥有哪些权限?

  • 首先,获取当前
    FileInfo
    对象描述的文件的访问控制列表(ACL)条目,该条目封装在FileSecurity对象中
  • 然后,我们使用
    GetAccessRules
    来获取与前面提到的
    FileSecurity
    对象关联的规则集合
  • 规则集合表示
    AuthorizationRule
    对象,这些对象是
    FileSystemAccessRule
    从中派生的,您可以查询这些对象以了解与文件相关的权限
代码段:检查test.txt是否具有读取权限(已使用.Net Core进行了测试)

使用系统;
使用System.IO;
使用System.Security.AccessControl;
使用System.Security.Principal;
使用System.Linq;
var MyPath=@“C:\Users\repos\test.txt”;
var fInfo=新文件信息(MyPath);
FileSecurity fSecurity=fInfo.GetAccessControl();
SecurityIdentifier usersSid=新的SecurityIdentifier(WellKnownSidType.BuiltinUsersSid,null);
FileSystemRights fileRights=FileSystemRights.Read | FileSystemRights.Synchronize//所有只读文件通常在允许访问时自动添加同步,请参阅下面的msdn文档链接
var rules=fSecurity.GetAccessRules(true,true,usersSid.GetType())。of type();
var hasRights=rules.Where(r=>r.FileSystemRights==fileRights).Any();
Nuget先决条件:


参考资料:

你说它不起作用到底是什么意思?没有引发异常,但你没有访问权限?@ricardopes完全正确。@TomTom询问如何检查文件是否可读并非离题……可能重复:你不能依赖此方法的结果。似乎没有别的办法,只能尝试用所需的访问权限打开文件。@Clint我会尽快打开(在接下来的24小时内)。不幸的是,由于整个科罗纳的情况,我的工作日非常繁忙,atm。我一试过就会告诉你,再次非常感谢你。可悲的是,这对我来说不起作用,也就是说,很明显,我应该先提到这一点:服务器运行在Linux上,而不是Windows上。所以当我运行您的代码时,我得到了:
未处理的异常。System.PlatformNotSupportedException:访问控制列表(ACL)API是Windows上资源管理的一部分,在此平台上不受支持。
@rasmus91,啊,这太糟糕了。非常感谢。我不太确定我是否应该接受你的答案(如果你说它有效,它可能有效),但我不能在我们的环境中使用它。
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Linq;

var MyPath = @"C:\Users\repos\test.txt";
var fInfo = new FileInfo(MyPath);

FileSecurity fSecurity = fInfo.GetAccessControl();

SecurityIdentifier usersSid = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
FileSystemRights fileRights = FileSystemRights.Read | FileSystemRights.Synchronize; //All read only file usually have Synchronize added automatically when allowing access, refer the msdn doc link below

var rules = fSecurity.GetAccessRules(true, true, usersSid.GetType()).OfType<FileSystemAccessRule>();
var hasRights = rules.Where(r => r.FileSystemRights == fileRights).Any();