2

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

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

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

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


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

FolderAcl.AddAccessRule(New FileSystemAccessRule(UserGroup,
                        FileSystemRights.ReadAndExecute,
                        InheritanceFlags.None, PropagationFlags.None,
                        AccessControlType.Allow))
FolderAcl.AddAccessRule(New FileSystemAccessRule(UserGroup,
                        FileSystemRights.Write,
                        InheritanceFlags.None, PropagationFlags.None,
                        AccessControlType.Allow))

FolderAcl.AddAccessRule(New FileSystemAccessRule(AdminGroup,
                        FileSystemRights.FullControl,
                        InheritanceFlags.None, PropagationFlags.None,
                        AccessControlType.Allow))

FolderAcl.SetAccessRuleProtection(True, False)


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

' same code as above except...
InheritanceFlags.None ---> InheritanceFlags.Container or InheritanceFlags.Object


我已经尝试过 InheritanceFlags 和 PropagationFlags 的其他组合,但还没有运气。

有任何想法吗?
谢谢,迈克

4

1 回答 1

1

更新:

您可以随时中断继承并决定保留继承的访问规则的副本或使用以下方法将其删除

SetAccessRuleProtection(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)

您可以使用WellKnownSid指定组并将其设置在具有继承的根文件夹中:

    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 访问权限,以及对管理员组对您的根文件夹以及所有子文件夹和文件的完全访问权限。

于 2012-07-19T01:59:31.973 回答