【问题标题】:Access Denied error while reading from filestream从文件流读取时访问被拒绝错误
【发布时间】:2013-11-13 01:42:39
【问题描述】:

我有一个将文件存储在 SQL 文件流中的 Intranet 应用程序。

在我的开发机器上,一切都像魅力一样。

我能够将文件上传和存储到 SQL 文件流 (AjaxUpload) 并能够下载它们。

在实时服务器上,我可以上传文件,删除它们,但是当我尝试从文件流下载文件时,我收到以下错误:


Access is denied

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ComponentModel.Win32Exception: Access is denied

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[Win32Exception (0x80004005): Access is denied]
   System.Data.SqlTypes.SqlFileStream.OpenSqlFileStream(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize) +1465594
   System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize) +398
   System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access) +27
   quotes_GetFileStream.quotes_GetFileStream_Load(Object sender, EventArgs e) +740
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3048

应用程序池设置为集成,V.4.0,我使用域用户进行身份认证回SQL。

我授予该用户 DB_Owner 对该应用程序的 SQL 数据库的权限。

甚至尝试给它所有的 SQL Server 角色,但我仍然收到上述错误。

当我将身份用户名更改为我的(我拥有域管理员权限)时,一切都在实时服务器上完美运行。

我缺少什么权利来授予该用户,以便他可以正确读取 SQL 文件流。

这是从文件流中获取文件并将其推送到浏览器的代码块,也许我在这里遗漏了一些东西(尽管一旦我修改了用户,它就很好用)。

Dim RecId As Integer = -1

    If Not IsNothing(Request.QueryString("ID")) And IsNumeric(Request.QueryString("ID")) Then
        RecId = CType(Request.QueryString("ID"), Integer)
    End If

    Dim ConString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
    Using Con As SqlConnection = New SqlConnection(ConString)
        Con.Open()
        Dim txn As SqlTransaction = Con.BeginTransaction()
        Dim Sql As String = "SELECT FileData.PathName() , GET_FILESTREAM_TRANSACTION_CONTEXT() as TransactionContext, [FileName], [FileExtension] FROM [QAttach] where RecId = @RecId"
        Dim cmd As SqlCommand = New SqlCommand(Sql, Con, txn)
        cmd.Parameters.Add("@RecID", Data.SqlDbType.Int).Value = RecId
        Dim Rdr As SqlDataReader = cmd.ExecuteReader()

        While Rdr.Read()
            Dim FilePath As String = Rdr(0).ToString()
            Dim objContext As Byte() = DirectCast(Rdr(1), Byte())
            Dim fname As String = Rdr(2).ToString()
            Dim FileExtension As String = Rdr(3).ToString()

            Dim sfs As SqlFileStream = New SqlFileStream(FilePath, objContext, FileAccess.Read)

            Dim Buffer As Byte() = New Byte(CInt(sfs.Length) - 1) {}
            sfs.Read(Buffer, 0, Convert.ToInt32(Buffer.Length))


            Response.Buffer = True
            Response.Charset = ""
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.ContentType = FileExtension
            Response.AddHeader("content-disposition", "attachment;filename=" & fname)
            Response.BinaryWrite(Buffer)
            Response.Flush()
            Response.End()

            sfs.Close()

        End While
    End Using

谢谢

奥伦

【问题讨论】:

  • 您为应用程序池设置的用户是否对您正在流式传输的文件具有读取权限?

标签: asp.net sql sql-server filestream


【解决方案1】:

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我能够通过确保用于访问数据库的帐户对正在访问的表中的 FILESTREAM 列具有 SELECT 和 UPDATE 权限来解决此问题。这是所需的 T-SQL 代码:

    GRANT SELECT, UPDATE ON OBJECT::[schema name].[table name]([file stream column name]) TO [the role/user to grant the rights to] AS [dbo];
    GO
    

    【讨论】:

      【解决方案3】:

      听起来更像是操作系统权限而不是 SQL Server 权限的问题。尝试向您的应用程序池使用的域帐户授予对创建文件流的路径的读/写访问权限

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-09
        • 1970-01-01
        • 2010-09-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多