SQL文件流身份验证最佳做法

tem*_*pid 5 sql filestream

我可以在本地很好地使用SQL Filestream,但是当我尝试将文件上传到使用SQL身份验证的远程SQL服务器时,出现“拒绝访问”异常。显然,SQL Filestream仅适用于Windows身份验证(Integrated Security = true),不适用于我们当前拥有的SQL身份验证。

没有人在生产环境中真正使用Windows身份验证,因此我只想知道如何克服这一限制。最佳做法是什么?

    public static void AddItem(RepositoryFile repository, byte[] data)
{
    using (var scope = new TransactionScope())
    {
        using (var db = new MyEntities()) // DBContext
        {
            db.RepositoryTable.AddObject(repository);
            db.SaveChanges();
        }

        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        using (var cmd = new SqlCommand(string.Format("SELECT Data.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM dbo.RepositoryTable WHERE ID='{0}'", repository.ID), con)) // "Data" is the column name which has the FILESTREAM. Data.PathName() gives me the local path to the file.
        {
            cmd.Connection.Open();
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    var path = reader.GetString(0);
                    var transactionContext = reader.GetSqlBytes(1).Buffer;
                    var fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write);

                    fileStream.Write(contents, 0, contents.Length); // I get the error at this line.
                    fileStream.Close();
                }
            }
        }

        scope.Complete();
    }
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*unt 5

使用FILESTREAM时,确实必须使用集成身份验证:

SQL Server 2008中的文件流存储

您需要确保在生产环境中运行生产应用程序的Windows帐户已作为登录名添加到SQL Server中,并且已被授予与该应用程序当前使用的SQL身份验证帐户相同的权限。

您还必须确保该帐户具有文件系统权限以写入FILESTREAM容器。