C# 判断用户是否对路径拥有访问权限

摘要:
如何获取当前系统用户对文件/文件夹的操作权限?FileSystemAccessRule继承自AuthorizationRule,并新增俩个属性AccessControlType--枚举Allow/DenyFileSystemRights--对文件的访问权限详细信息,可见下面列表:1///定义要创建访问和审核规则时使用的访问权限。

如何获取当前系统用户对文件/文件夹的操作权限?

1.获取安全信息DirectorySecurity

DirectorySecurity fileAcl = Directory.GetAccessControl(folder);

通过Directory.GetAccessControl获取文件夹的权限/安全信息

详细介绍,可参考MSDN官方文档

对文件/文件夹权限的详细操作,可参考一篇博客C#文件夹权限操作

2. 获取文件夹访问权限列表FileSystemAccessRule

var rules = fileAcl.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)).OfType<FileSystemAccessRule>().ToList();

GetAccessRules()方法返回的是AuthorizationRule集合,此处只需要获取文件权限。

FileSystemAccessRule继承自AuthorizationRule,并新增俩个属性

  • AccessControlType -- 枚举 Allow/Deny
  • FileSystemRights -- 对文件的访问权限详细信息(读/写等),可见下面列表:
1   /// <summary>定义要创建访问和审核规则时使用的访问权限。</summary>
2 [Flags]
3   public enumFileSystemRights
4 {
5     ReadData = 1,
6     ListDirectory = ReadData, //0x00000001
7     WriteData = 2,
8     CreateFiles = WriteData, //0x00000002
9     AppendData = 4,
10     CreateDirectories = AppendData, //0x00000004
11     ReadExtendedAttributes = 8,
12     WriteExtendedAttributes = 16, //0x00000010
13     ExecuteFile = 32, //0x00000020
14     Traverse = ExecuteFile, //0x00000020
15     DeleteSubdirectoriesAndFiles = 64, //0x00000040
16     ReadAttributes = 128, //0x00000080
17     WriteAttributes = 256, //0x00000100
18     Delete = 65536, //0x00010000
19     ReadPermissions = 131072, //0x00020000
20     ChangePermissions = 262144, //0x00040000
21     TakeOwnership = 524288, //0x00080000
22     Synchronize = 1048576, //0x00100000
23     FullControl = Synchronize | TakeOwnership | ChangePermissions | ReadPermissions | Delete | WriteAttributes | ReadAttributes | DeleteSubdirectoriesAndFiles | Traverse | WriteExtendedAttributes | ReadExtendedAttributes | CreateDirectories | CreateFiles | ListDirectory, //0x001F01FF
24     Read = ReadPermissions | ReadAttributes | ReadExtendedAttributes | ListDirectory, //0x00020089
25     ReadAndExecute = Read | Traverse, //0x000200A9
26     Write = WriteAttributes | WriteExtendedAttributes | CreateDirectories | CreateFiles, //0x00000116
27     Modify = Write | ReadAndExecute | Delete, //0x000301BF
28   }
View Code

因为AuthorizationRule中,IdentityReference对应权限的用户/用户组标识,格式为:"MYDOMAINMyAccount"

所以,如通过当前系统用户名与IdentityReference匹配,即可获取FileSystemAccessRule权限。如何获取用户名,见下一段落

3. 获取当前系统用户名/用户组

通过 System.Environment.UserDomainName 和 System.Environment.UserName 取得当前用户名

对当前系统用户名/用户组的其它操作,可参考

因此,将Path.Combine(Environment.UserDomainName, Environment.UserName)与IdentityReference.Value比较,获取当前用户对文件夹的权限信息

详细实现如下:

1     /// <summary>
2     ///检查当前用户是否拥有此文件夹的操作权限
3     /// </summary>
4     /// <param name="folder"></param>
5     /// <returns></returns>
6     public static bool HasOperationPermission(stringfolder)
7 {
8         var currentUserIdentity =Path.Combine(Environment.UserDomainName, Environment.UserName);
9 
10         DirectorySecurity fileAcl =Directory.GetAccessControl(folder);
11         var userAccessRules = fileAcl.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)).OfType<FileSystemAccessRule>().Where(i=>i.IdentityReference.Value==currentUserIdentity).ToList();
12 
13         return userAccessRules.Any(i => i.AccessControlType ==AccessControlType.Deny);
14     }

获取文件夹是否有删除权限(仅删除空文件夹):

1     /// <summary>
2 /// 检查当前用户是否拥有此文件夹的删除操作权限
3     /// </summary>
4     /// <param name="folder"></param>
5     /// <returns></returns>
6 public static bool HasDeleteOperationPermission(string folder)
7 {
8 var currentUserIdentity = Path.Combine(Environment.UserDomainName, Environment.UserName);
9 
10 DirectorySecurity fileAcl = Directory.GetAccessControl(folder);
11         var userAccessRules = fileAcl.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)).OfType<FileSystemAccessRule>().Where(i => i.IdentityReference.Value == currentUserIdentity).ToList();
12 
13 if (userAccessRules.Count > 0 &&
14 userAccessRules.Any(i => (i.FileSystemRights & FileSystemRights.Delete) != 0 && i.AccessControlType == AccessControlType.Allow))
15 {
16 
17 return true;
18 }
19 return false;
20     }

免责声明:文章转载自《C# 判断用户是否对路径拥有访问权限》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇web中打开word为什么老是提示无法打开宏储存?Lasso估计论文学习笔记(一)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

连载:面向对象葵花宝典:思想、技巧与实践(1)

史前时代:面向机器 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的指令和数据。简单来说,就是直接编写0和1的序列来代表程序语言。例如:使用0000 代表 加载(LOAD),0001 代表 存储(STORE)等。  机器语言由机器直接执行,速度快,但一个很明显的缺点就是:写起来实在是太困难了,一旦你发现自己写错了,改起来...

ubuntu下安装五笔

安装五笔输入法 | Install wubi 1、在终端窗口中输入如下命令: sudo apt-get install ibus-table-wubi 系统分析软件包列表,自动下载所需软件包,并安装。 2、右击输入法图标,在菜单中选择“重新启动”。  3、点击菜单“系统” --> “首选项”  --> “键盘输入法”。添加“五笔86...

Python+gitlab实现分支批量合并打tag

  最近公司代码托管有SVN迁移到Gitlab,每次版本发布都要将代码合并至master分支且打tag,应用较多重复且繁琐,于是使用Python+gitlab实现分支批量合并打tag; 参考 python-gitlab文档 环境准备: 1.Python3 2.pip install python-gitlab token:gitlab用户令牌,生成如下:...

OpenNESS,开源的边缘网络服务平台

目录 文章目录 目录 参考文章 OpenNESS 的电梯间演讲 OpenNESS 与 ETSI MEC Edge Controller Software 的功能清单 Edge Platform Software 的功能清单 OpenNESS 的部署方案 On-Premise Edge Deployment Network Edge Deploym...

玩转StyleGan2:手把手教你安装并运行项目!

时间过得是真快,离上次分享《用AI生成一堆漂亮且独一无二的女朋友!》已经快过去半个多月了。光分享效果,而不分享如何实现,这不是我的风格。所以从今天开始,我会带大家一起玩转这个强大的高清人脸生成项目(不懂开源项目的可以理解为软件)。 目前规划了几个主题 1. 安装并运行 2. 虚拟人脸编辑,操控虚拟人物表情 3. 真实人脸编辑,操控任何人的表情。 4. 云端...

vue 服务端渲染 vs 预渲染(1)

服务端渲染: 1、将完整的html输出到客户端 2、要使用通用代码 优点 :  1、首次渲染快(无需等所有的js都完成下载)   2、利于seo 缺点:  1、更多的服务器负载 2、开发受限 3、需要处于node.js/php server 运行环境 预渲染:  1、使用少数营销页的seo   2、生成对特定路由静态的html文件 优点:   1、预渲染更...