Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15889

SqlFileStream FileAccess.Read Access is Denied

$
0
0

Hello,

I have searched and read numerous posts similar to this situation, but none of them seem to apply.

My basic problem is this: I cannot seem to open the SqlFileStream on an ASP.Net webpage to read FileStream data to try to stream video. I get a "Access is Denied" error message.

Here's an overview of  my setup (which as been running for 3 years, the on new addition is the filestream "streaming"):

Server 2003 IIS server with Kerberos authentication to a separate 2003 Server running SQL Server 2008. (this was an old battle I fought long ago in order to use integrated security across separate web and Db servers... See Kerberos double-hop scenario)  Due to that, I believe I'm already using integrated security and I've tried to verify that along the way with a couple response.write to show the system_user returned by SQL as well as the current WindowsIdentity.  This user has file-level access to the filestream directory, which sits on the physical Db server.  Also, logged into SQL as this windows user, I am able to query the filestream binary data through t-sql, the only place I get this access is denied error is when I try to open the file through the filestream in asp.net/c#.  Despite the impersonation I believe I have going on here, it's making me wonder if/how the Application Pool Identity plays into this. I figured if I was hitting the DB as Windows account: MyDomain/MyUser, then I'd already taken care of the impersonation issue that is frequently cited as the issue. Unless there is more to it that no one seems to mention?? Could the separate server/Kerberos issue be coming back to haunt me?

My user account has already uploaded this filestream table, albeit not through a similar write operation, but through T-SQL. Due to the slowness of uploading large files this way, I'm going to stream the write too once this is solved.

Here's what I got:

(this page is a test for one which will feed a Silverlight mediaelement a video feed) I've traced my issue down to the line:

 SqlFileStream sfs = new SqlFileStream(filePath, txContext, FileAccess.Read);

<add name="ConnectionString" 
            connectionString="Data Source=MyServer; Initial Catalog=MyDb;Integrated Security=true"
      providerName="System.Data.SqlClient" />

protected void Page_Load(object sender, EventArgs e) { string VideoID; try { VideoID = Request.QueryString["vi"].ToString(); } catch { VideoID = "B119C03D-92DD-E211-AF66-00237D654CCC"; } //Create and open a database connection SqlConnection cn = new SqlConnection(); cn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); cn.Open(); //Create a Command object SqlCommand cmd = new SqlCommand(); cmd.CommandText = "fcc.sspGetVideoPath"; cmd.Connection = cn; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@FileId", VideoID); //Begin a transaction SqlTransaction trn = cn.BeginTransaction("VideoTran"); cmd.Transaction = trn; string filePath = string.Empty; string sysUser = string.Empty; byte[] txContext = null; try { //Execute the command SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { txContext = (reader["txContext"] as byte[]); filePath = reader["filePath"].ToString(); sysUser = reader["sysUser"].ToString(); } reader.Close(); Response.Write("**FileStream: " + filePath.ToString()); Response.Write("<br />**Connecting to SQL As: " + sysUser.ToString()); Response.Write("<br />**C# User: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()); //Open the FILESTREAM data file for reading /*"Access is Denied" Error Is Thrown Here*/ SqlFileStream sfs = new SqlFileStream(filePath, txContext, FileAccess.Read); //Start transferring data into filestream data file Response.BufferOutput = false; Response.ContentType = "video/x-ms-wmv"; BinaryWriter bwr = new BinaryWriter(Response.OutputStream); const int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; int byteCount = sfs.Read(buffer, 0, bufferSize); while (byteCount == bufferSize) { bwr.Write(buffer, 0, byteCount); byteCount = sfs.Read(buffer, 0, bufferSize); } //Close the files Response.End(); bwr.Close(); sfs.Close(); } catch (Exception ex) { Response.Write("<br />**Error: " + ex.Message.ToString()); Response.Write("<br />**Stack Trace: " + ex.StackTrace.ToString()); try { Response.Write("<br />**Inner Ex: " + ex.InnerException.Message.ToString()); } catch { } } finally { // Commit the transaction and close connection cmd.Transaction.Commit(); cn.Close(); }


 

Any direction is appreciated. Thanks in Advance.

 

 

 




Viewing all articles
Browse latest Browse all 15889

Trending Articles