VB.NET应用程序正在目录上设置受限制的文件权限,这不正确地限制了同一目录中用户创建的文件

VB.NET应用程序正在目录上设置受限制的文件权限,这不正确地限制了同一目录中用户创建的文件,vb.net,permissions,file-permissions,ntfs,folder-permissions,Vb.net,Permissions,File Permissions,Ntfs,Folder Permissions,我的VB.NET应用程序构建了一个目录树,具有受限访问权限 访问权限应为普通用户不能删除或重命名现有树。但用户可以在树中的任何位置添加新文件/文件夹。任何用户都可以完全修改用户创建的文件/文件夹 我遇到的问题是设置访问权限,使应用程序创建的文件/文件夹无法更改,但用户创建的文件/文件夹可以由任何用户更改 当前发生的情况是,应用程序生成的文件/文件夹行为正确。但当用户创建自己的文件/文件夹时,该文件/文件夹的权限仅列出当前用户。所以其他用户(甚至应用程序创建的文件/文件夹的系统管理员)无法查看或修

我的VB.NET应用程序构建了一个目录树,具有受限访问权限

访问权限应为普通用户不能删除或重命名现有树。但用户可以在树中的任何位置添加新文件/文件夹。任何用户都可以完全修改用户创建的文件/文件夹

我遇到的问题是设置访问权限,使应用程序创建的文件/文件夹无法更改,但用户创建的文件/文件夹可以由任何用户更改

当前发生的情况是,应用程序生成的文件/文件夹行为正确。但当用户创建自己的文件/文件夹时,该文件/文件夹的权限仅列出当前用户。所以其他用户(甚至应用程序创建的文件/文件夹的系统管理员)无法查看或修改此用户创建的文件/文件夹


当前代码:(当用户自己创建文件/文件夹时,此代码不授予UserGroup或AdminGroup访问权限,仅授予刚刚创建文件/文件夹的用户)


另一种尝试:(当用户自己创建文件/文件夹时,此代码授予用户组和AdminGroup访问权限,但不授予用户访问权限。用户在UserGroup中,但UserGroup没有删除或修改权限,因此如果用户创建文件/文件夹,他们甚至无法命名它。)


我尝试过继承标志和传播标志的其他组合,但还并没有成功

有什么想法吗?
谢谢
迈克

更新:

您可以随时中断继承,并决定保留继承的访问规则的副本,或使用

SetAccessRuleProtection(True, True)
第一个布尔参数(如果为true)会破坏继承保护,第二个布尔参数(如果为true)会保留访问规则的副本,以便您只能删除不需要的规则

以下示例应反映您的文件夹结构,如注释所示:

 ' folder structure
        '
        '---Level1
        '     |
        '     ---Level2
        '          |
        '          ---Level3

        'set access rules at level1 with inheritance

        Dim Level1DirSec As DirectorySecurity = Directory.GetAccessControl("c:\level1")

        Level1DirSec.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.BuiltinAdministratorsSid, Nothing),
         FileSystemRights.FullControl,
         InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
         PropagationFlags.None,
         AccessControlType.Allow))

        Level1DirSec.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.AuthenticatedUserSid, Nothing),
          FileSystemRights.ReadAndExecute,
          InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
          PropagationFlags.None,
          AccessControlType.Allow))

        Directory.SetAccessControl("c:\level1\", Level1DirSec)


        ' break inheritance at level3 and remove access rule for authenticated user group

        Dim Level3DirSec As DirectorySecurity = Directory.GetAccessControl("c:\level1\level2\level3")

        Level3DirSec.SetAccessRuleProtection(True, True)

        Level3DirSec.RemoveAccessRuleAll(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.AuthenticatedUserSid, Nothing), FileSystemRights.ReadAndExecute, AccessControlType.Allow))

        Directory.SetAccessControl("c:\level1\level2\level3", Level3DirSec)

您可以使用指定组并在具有继承的根文件夹上进行设置:

    FolderAcl.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, Nothing),
             FileSystemRights.ReadAndExecute,
             InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
             PropagationFlags.None,
             AccessControlType.Allow))

    FolderAcl.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, Nothing),
             FileSystemRights.FullControl,
             InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
             PropagationFlags.None,
             AccessControlType.Allow))

这将为所有经过身份验证的用户提供r/w访问权限,并为管理员组提供对根文件夹、所有子文件夹和文件的完全访问权限。

感谢您的回复。但问题是,这将为树中登录的用户提供对所有文件夹的写访问权限。应用程序会创建用户可能无权访问的更深层次的文件夹。继承登录用户的访问权限会违反此规则。我想要一个这样的树:-第1层:读/写用户组,完整的管理员-第2层:读/写用户组,完整的管理员-第3层:完整的管理员应用程序使这样一个树。如果用户组中的某个人而不是管理员在LVL2中创建了一个新文件/文件夹,我希望所有用户都能完全访问该文件/文件夹。我要找的是一个与umask等效的文件/文件夹。我现在看到,当用户在Windows中创建新文件/目录时,它会从父目录继承其权限。我希望用户创建的文件可以由该用户删除,而不允许用户删除父目录。似乎无法让此用户生成的文件/目录继承父权限并为用户添加删除权限。
 ' folder structure
        '
        '---Level1
        '     |
        '     ---Level2
        '          |
        '          ---Level3

        'set access rules at level1 with inheritance

        Dim Level1DirSec As DirectorySecurity = Directory.GetAccessControl("c:\level1")

        Level1DirSec.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.BuiltinAdministratorsSid, Nothing),
         FileSystemRights.FullControl,
         InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
         PropagationFlags.None,
         AccessControlType.Allow))

        Level1DirSec.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.AuthenticatedUserSid, Nothing),
          FileSystemRights.ReadAndExecute,
          InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
          PropagationFlags.None,
          AccessControlType.Allow))

        Directory.SetAccessControl("c:\level1\", Level1DirSec)


        ' break inheritance at level3 and remove access rule for authenticated user group

        Dim Level3DirSec As DirectorySecurity = Directory.GetAccessControl("c:\level1\level2\level3")

        Level3DirSec.SetAccessRuleProtection(True, True)

        Level3DirSec.RemoveAccessRuleAll(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.AuthenticatedUserSid, Nothing), FileSystemRights.ReadAndExecute, AccessControlType.Allow))

        Directory.SetAccessControl("c:\level1\level2\level3", Level3DirSec)
    FolderAcl.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, Nothing),
             FileSystemRights.ReadAndExecute,
             InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
             PropagationFlags.None,
             AccessControlType.Allow))

    FolderAcl.AddAccessRule(New FileSystemAccessRule(New System.Security.Principal.SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, Nothing),
             FileSystemRights.FullControl,
             InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit,
             PropagationFlags.None,
             AccessControlType.Allow))