DirectorySecurity未正确设置权限

我有一个C#代码,它创建一个文件夹并设置一些权限。 这是代码示例:

static void Main(string[] args){ Directory.CreateDirectory("C:\\vk07"); DirectorySecurity dirSec = Directory.GetAccessControl("C:\\vk07"); dirSec.AddAccessRule(new FileSystemAccessRule("INTRANET\\fGLBChorusUsers", FileSystemRights.ReadAndExecute, AccessControlType.Allow)); Directory.SetAccessControl("C:\\vk07", dirSec); } 

当我检查在上面创建的文件夹上设置的权限,而不是具有读取和修改(这是我在代码中设置的)时,它仅显示“特殊权限”已选中。

有人可以帮我这个吗? 我是ACL的新手,所以不太了解它。

FileSystemRights.ReadAndExecute不允许您修改。 这是只读的。 你需要FileSystemRights.Modify的全部范围。 您可能想要查看可用选项。

以下是上述示例:

 String dir = @"C:\vk07"; Directory.CreateDirectory(dir); DirectoryInfo dirInfo = new DirectoryInfo(dir); DirectorySecurity dirSec = dirInfo.GetAccessControl(); dirSec.AddAccessRule(new FileSystemAccessRule("INTRANET\\fGLBChorusUsers",FileSystemRights.Modify,AccessCo‌ntrolType.Allow)); dirInfo.SetAccessControl(dirSec); 

我遇到了同样的问题,实际的原因是,如果你从另一篇文章中查看该网络服务图片,它只适用于文件。 如果他们说“此文件夹,子文件夹和文件”,则基本权限将仅显示在第一张图片上。为此,您需要设置两个标志-InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit。

  Try 'If destination directory does not exist, create it first. If Not Directory.Exists(path) Then Directory.CreateDirectory(path) Dim dir As New DirectoryInfo(path) Dim dirsec As DirectorySecurity = dir.GetAccessControl() 'Remove inherited permissions dirsec.SetAccessRuleProtection(True, False) 'create rights, include subfolder and files to be inherited by this Dim Modify As New FileSystemAccessRule(username, FileSystemRights.Modify, InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow) Dim Full As New FileSystemAccessRule(admingroup, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow) dirsec.AddAccessRule(Modify) dirsec.AddAccessRule(Full) 'Set dir.SetAccessControl(dirsec) Catch ex As Exception MsgBox(ex.Message) End Try 

这段代码适合我:

  security.AddAccessRule( new FileSystemAccessRule( "domain\\login", FileSystemRights.Modify, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow )); 

我也有这个问题。 执行以下代码后:

 var security = Directory.GetAccessControl(folderPath); security.AddAccessRule( new FileSystemAccessRule( new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null), FileSystemRights.Modify, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow ) ); Directory.SetAccessControl(folderPath, security); 

…然后, folderPath的属性对话框将显示如下:

文件夹属性对话框

如您所述,仅选中“特殊权限”,但如果单击“高级”,则会看到:

高级安全设置对话框

请注意,在此对话框中,NETWORK SERVICE具有修改权限。

看起来好像以编程方式设置权限时,Windows不会在文件夹属性对话框中显示这些权限,但它们仍然存在于高级安全设置下。 我还确认我的Window服务(作为NETWORK SERVICE运行)然后能够访问folderPath中的文件。

我有相同的代码在VB中工作,设置FileSystemRights.FullControl。

  Dim fsRule As FileSystemAccessRule = New FileSystemAccessRule(sid, FileSystemRights.FullControl, (InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit), PropagationFlags.None, AccessControlType.Allow)