c# - DirectorySecurity 未正确设置权限

标签 c#

我有一个 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,不是很了解。

最佳答案

我遇到了同样的问题,实际原因是如果您查看另一篇文章中的网络服务图片,它仅适用于文件。如果第一张图片显示“此文件夹、子文件夹和文件”,则基本权限只会显示在第一张图片上。为此,您需要设置两个标志 -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

关于c# - DirectorySecurity 未正确设置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5952831/

相关文章:

c# - 多重赋值(字段=属性=值)

c# - 为什么在.Net中HashHelpers.IsPrime是这样实现的呢?

c# - .NET compact framework - 检测是否在模拟器下?

c# - 如何使用 lambda 和非 lambda linq 有条件地排序?

c# - Rightmove 实时数据馈送 (RTDF) asp.net 的集成

c# - 如何使用 Win2D 模拟 DeviceLost 事件?

c# - 将具有空值的参数添加到 sqldatasource 控件的更好方法

c# - Math.Net 数值中的多元回归

C# 使用 LINQ 读取和过滤 CSV 文件

c# - 为什么在使用 TypesToScan() 后 NServiceBus 配置被破坏