【问题标题】:Access denied when copying file from File Table unc path to a file server将文件从文件表 unc 路径复制到文件服务器时访问被拒绝
【发布时间】:2019-12-14 09:08:30
【问题描述】:

我有一个具有完全非事务性访问权限的文件表。因此,它可以像任何文件服务器一样被浏览,也可以通过 T-SQL 直接插入。我编写了一个简单的 SQL CLR C# 来将文件从文件表复制到文件服务器。执行复制的用户是对 sql server(因此是文件表)和文件共享具有管理员权限的 sql 服务帐户。没有真正的权限错误。

```try
    {
        using (new ImpersonationNamespace.Impersonation(domain, username, password))
        {

            if (!Directory.Exists(sourceDirectory))
            {
                throw (new Exception("Source Directory: '" + sourceDirectory.ToString() + "' does not exist or user: "+ WindowsIdentity.GetCurrent().Name+" does not not have rights."));
            }
            if (!Directory.Exists(targetDirectory))
            {
                throw (new Exception("Target Directory: '" + targetDirectory.ToString() + "' does not exist or user: " + WindowsIdentity.GetCurrent().Name + " does not not have rights."));
            }
            if (!File.Exists(sourcePath))
            {
                throw (new Exception("Source File: '" + sourceFileName.ToString() + "' does not exist or user: " + WindowsIdentity.GetCurrent().Name + " does not not have rights."));
            }
            if (File.Exists(targetPath) && overwrite == false)
            {
                throw (new Exception("Target File: '" + targetFileName.ToString() + "' already exists in targeted directory: '" + targetDirectory.ToString() + "'"));
            }

            File.Copy(sourcePath, targetPath, overwrite);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
```

消息 6522,级别 16,状态 1,过程 dbo.FileCopy_CLR,第 0 行 [批处理开始第 30 行] 在执行用户定义的例程或聚合“FileCopy_CLR”期间发生 .NET Framework 错误: System.Exception:源目录:'\sqlserver\filestreamname\FilestreamDirectoryName\Filetable\FlowInput\13143\' 不存在或用户:域\sqlserverserviceaccount 没有权限。 系统异常: 在 StoredProcedures.FileCopy(字符串 sourceDirectory,字符串 targetDirectory,字符串 sourceFileName,字符串 targetFileName,布尔覆盖,字符串域,字符串用户名,字符串密码) .

【问题讨论】:

    标签: sql-server permissions filetable


    【解决方案1】:

    不确定这是否是一个好的答案,但... 我重命名了 Filetable_Directory 以匹配文件表名称本身。 我在所有目录上更改了 Read_Only 位。 尽管所有文件流设置都是正确的,但我还是退回了 sql 服务。 我还在 C# 中使用 path.combine 来确保正确形成字符串等。

    然后我通过管理工作室再次测试了该程序并且它有效,所以我认为这一切都很重要。但它没有。事实证明,我正在使用的服务器名称案例来测试 proc 是问题所在。

    Directory.Exists() 和 File.Copy() 在我的工作站上运行良好,因为我的测试使用的是 \Servername.domain.com\Filetablerootdirectories....

    调用同一 proc 的应用程序正在使用 REPLACE 字符串重述生产路径(以避免影响生产)并将名称硬编码为 \SERVERNAME\FileTablerootdirectories...

    所以问题的可文件性是无关紧要的。我不知道为什么 System.IO 库在大小写和完全限定等方面表现如此。但如果你遇到这个问题,它与 Filestream 或 SQL Server 引擎拦截权限等无关。这一直都很好。

    幸运的是,我正在从文件表中复制文件,并将服务器名称安全地硬编码为可用的名称。此外,目标目录同样是固定的,所以我可以安全地按原样使用它。但是,如果我想要一个 clr procs 工具带在 SQL Server 中执行文件系统实用程序,则必须加强一点以使其更加健壮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2017-07-29
      • 2021-05-03
      • 1970-01-01
      • 2019-12-19
      • 2011-05-21
      • 2020-07-26
      相关资源
      最近更新 更多