Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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# 帐户具有权限,但未获取权限错误_C#_Sql Server - Fatal编程技术网

C# 帐户具有权限,但未获取权限错误

C# 帐户具有权限,但未获取权限错误,c#,sql-server,C#,Sql Server,我正在使用c#中的以下脚本来确定sqlserver服务帐户是否对目录具有权限 public static bool userhasaccess(string username, string path) { DirectorySecurity dSecurity = Directory.GetAccessControl(path); bool control_allowed = false; bool control_denied = false; foreach

我正在使用c#中的以下脚本来确定sqlserver服务帐户是否对目录具有权限

public static bool userhasaccess(string username, string path)
{
    DirectorySecurity dSecurity = Directory.GetAccessControl(path);
    bool control_allowed = false;
    bool control_denied = false;
    foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
    {                
        if (rule.IdentityReference.Value.ToLower() == username.ToLower())
        {
            if (rule.FileSystemRights == FileSystemRights.Read
                    || rule.FileSystemRights == FileSystemRights.FullControl
                    || rule.FileSystemRights == FileSystemRights.ReadAndExecute
                    || rule.FileSystemRights == FileSystemRights.ReadData
                    )
            {
                if (rule.AccessControlType == AccessControlType.Allow)
                    control_allowed = true;
                if (rule.AccessControlType == AccessControlType.Deny)
                    control_denied = true;
            }

        }
    }
    return (control_denied ? false : control_allowed);
}
这并不完美,因为它可能忽略组,但我已经使用我的特定示例详细介绍了代码,并且没有拒绝对我的文件夹的权限。(\\server\directory)

然后,我执行一个SqlCommand
restore headernoly from disk=N'\\server\directory\file.bak'
,该命令应给出此目录中文件的还原头

public static bool userhasaccess(string username, string path)
{
    DirectorySecurity dSecurity = Directory.GetAccessControl(path);
    bool control_allowed = false;
    bool control_denied = false;
    foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
    {                
        if (rule.IdentityReference.Value.ToLower() == username.ToLower())
        {
            if (rule.FileSystemRights == FileSystemRights.Read
                    || rule.FileSystemRights == FileSystemRights.FullControl
                    || rule.FileSystemRights == FileSystemRights.ReadAndExecute
                    || rule.FileSystemRights == FileSystemRights.ReadData
                    )
            {
                if (rule.AccessControlType == AccessControlType.Allow)
                    control_allowed = true;
                if (rule.AccessControlType == AccessControlType.Deny)
                    control_denied = true;
            }

        }
    }
    return (control_denied ? false : control_allowed);
}
这给了我以下错误:

Cannot open backup device '\\server\directory\file.bak'. Operating system error 5(Access is denied.).
RESTORE HEADERONLY is terminating abnormally.

根据GetAccessRules,当此帐户完全控制此目录时,为什么会发生这种情况?

检查该位置的共享权限而不仅仅是目录权限。这就是您的意思吗?是的,您可以使用类似的方法来查找共享权限。我已尝试使用上面的链接,但我一直收到错误“此共享资源不存在”。你有什么进一步的建议吗?作为该链接的第一条评论,我建议你看看这个链接