Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我可以写入文件夹,但DirectorySecurity.GetAccessRules不';我的用户没有任何规则_C# - Fatal编程技术网

C# 为什么我可以写入文件夹,但DirectorySecurity.GetAccessRules不';我的用户没有任何规则

C# 为什么我可以写入文件夹,但DirectorySecurity.GetAccessRules不';我的用户没有任何规则,c#,C#,我正在编写一个工具,用于检查用户是否有权读取、写入和删除给定文件夹集中的文件 我知道我的用户可以使用windows资源管理器在这些文件夹中执行所有这些操作,因为我已经手动尝试过,并且我的程序正在以我的身份运行 当我打电话时 AuthorizationRuleCollection rules = Directory.GetAccessControl(somePath).GetAccessRules(true, true, typeof(NTAccount)); 规则列表,是针对我绝对不属于的团体

我正在编写一个工具,用于检查用户是否有权读取、写入和删除给定文件夹集中的文件

我知道我的用户可以使用windows资源管理器在这些文件夹中执行所有这些操作,因为我已经手动尝试过,并且我的程序正在以我的身份运行

当我打电话时

AuthorizationRuleCollection rules = Directory.GetAccessControl(somePath).GetAccessRules(true, true, typeof(NTAccount));
规则列表,是针对我绝对不属于的团体。事实上,它们位于另一个域上(除了BUILTIN\Administrators,我不在其中)

当我使用windows资源管理器检查我对文件夹的有效访问权限时(右键单击->属性->安全->高级->有效访问),它表示我根本没有任何访问权限。这很奇怪,因为我肯定可以在文件夹中创建新文件

我已将我的代码包括在下面:

private static void CheckPermissions(string path, FileSystemRights desiredRights)
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        try
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            AuthorizationRuleCollection rules = Directory.GetAccessControl(row.Path).GetAccessRules(true, true, typeof(SecurityIdentifier));

            FileSystemRights allAllowRights = 0;
            FileSystemRights allDenyRights = 0;

            foreach (FileSystemAccessRule rule in rules)
            {
                if (identity.Groups.Contains(rule.IdentityReference) || identity.Owner.Equals(rule.IdentityReference) || rule.IdentityReference.ToString() == identity.User.Value) 
                {
                    if (rule.AccessControlType == AccessControlType.Deny)
                    {
                        allDenyRights |= rule.FileSystemRights;
                    }
                    else if (rule.AccessControlType == AccessControlType.Allow)
                    {
                        allAllowRights |= rule.FileSystemRights;
                    }
                }
            }

            if (allDenyRights.HasFlag(desiredRights)) 
            {
                Console.WriteLine($"FAILURE, User {identity.Name} is explicitly DENIED a required permission on, {path}");
            }

            if (!allAllowRights.HasFlag(desiredRights)) 
            {
                Console.WriteLine($"FAILURE, User {identity.Name} missing required ALLOW permission on, {path}");
            }

        }
        catch (UnauthorizedAccessException ex)
        {
            Console.WriteLine($"FAILURE, User {identity.Name} does not have any access to {path}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"FAILURE, User {identity.Name} something went wrong while checking permissions {path}");
        }
    }