2010-08-17 67 views
9

我从下面这段代码得到一个重复FileSystemAccessRule:重复GetAccessRules,FileSystemAccessRule条目

C:\inetpub\wwwroot\AspInfo\Account 
BUILTIN\IIS_IUSRS : Allow : ReadAndExecute, Synchronize 
BUILTIN\IIS_IUSRS : Allow : -1610612736 
NT SERVICE\TrustedInstaller : Allow : FullControl 
NT SERVICE\TrustedInstaller : Allow : 268435456 

,我不能工作了什么,或者为什么它是。

并显示的权限不匹配我可以看到文件FileManager属性。 例如,如何从这个或类似的迭代中找到“列出文件夹内容”权限。如果有人知道.NET文档中的一个例子,它会有所帮助。

protected void directoryInfo() 
{ 
    var di = new DirectoryInfo(Server.MapPath("/")); 
    foreach (DirectoryInfo dir in di.GetDirectories()) 
    { 
    Response.Write(dir.FullName + "<br/>"); 
    DirectorySecurity ds = dir.GetAccessControl(); 
    foreach (FileSystemAccessRule fsar in ds.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) 
    { 
     string userName = fsar.IdentityReference.Value; 
     string userRights = fsar.FileSystemRights.ToString(); 
     string userAccessType = fsar.AccessControlType.ToString(); 
     Response.Write(userName + " : " + userAccessType + " : " + userRights + "<br/>"); 
    } 
    } 
} 

回答

13

对于继承的规则和显式设置在该文件夹中的规则,您将得到单独的规则条目。根据每个规则的传播设置也有差异。例如,可以将一组权限设置为传播到子文件夹,并将不同的设置设置为文件夹内的文件。您的代码也在您似乎只想访问权限(DACL)的文件夹上获取审核规则(SACL)。

试试这个:

protected void directoryInfo() 
{ 
    var di = new DirectoryInfo(Server.MapPath("/")); 
    foreach (DirectoryInfo dir in di.GetDirectories()) 
    { 
    Response.Write(dir.FullName + "<br/>"); 
    DirectorySecurity ds = dir.GetAccessControl(AccessControlSections.Access); 
    foreach (FileSystemAccessRule fsar in ds.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) 
    { 
     string userName = fsar.IdentityReference.Value; 
     string userRights = fsar.FileSystemRights.ToString(); 
     string userAccessType = fsar.AccessControlType.ToString(); 
     string ruleSource = fsar.IsInherited ? "Inherited" : "Explicit"; 
     string rulePropagation = fsar.PropagationFlags.ToString(); 
     string ruleInheritance = fsar.InheritanceFlags.ToString(); 
     Response.Write(userName + " : " + userAccessType + " : " + userRights + " : " + ruleSource + " : " + rulePropagation + " : " + ruleInheritance + "<br/>"); 
    } 
    } 
} 

你看到的ReadAndExecute权限包括 “列出文件夹目录” 权限。您可以使用FileSystemRights枚举中的相应标志来检查单个权限。例如:

if (fsar.FileSystemRights && FileSystemRights.ListDirectory) 
    Console.WriteLine("Has List Directory permission"); 
+0

不允许使用'FileSystemRights'进行'if'评估。 – Thomas 2015-09-23 13:56:11

+0

应该能够简单地检查'if(FileSystemRights.ListDirectory){...}'。 https://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesystemrights(v=vs.110).aspx你不应该看看你是否有给定目录上的'FileSystemRights' - if你在循环中,你有目录。如果你有目录,你有'FileSystemAccessRule'属性。如果你在'FileSystemAccessRule'属性的循环中,你将拥有'FileSystemRights'属性,保证。 – vapcguy 2017-03-30 00:06:56

+0

@vapcguy如何排除FileSystemAuditRule项目? – Chalky 2017-05-11 21:57:53