Securing Large Downloads Using C# and IIS 7

前端 未结 4 1140
[愿得一人]
[愿得一人] 2021-01-17 23:49

Here\'s the setup:

  • 1 web server running a C# app to which my users (stored in a MySQL database on said server) authenticate.

  • 1 file server

相关标签:
4条回答
  • 2021-01-17 23:56

    You may be interested in Microsoft's Background Intelligent Transfer Service (BITS).

    http://msdn.microsoft.com/en-us/library/bb968799%28VS.85%29.aspx

    Version 2.5 introduced HTTP authentication via certificates.

    0 讨论(0)
  • 2021-01-17 23:57

    You know what? The KB article is poo. Here is my official recommendation:

    public void StreamFile(string filePath)
    {
        string fileName = Path.GetFileName(filePath);
    
        using (var fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            var contentLength = fStream.Length;
    
            if (Request.UserAgent.Contains("MSIE"))
            {
                Response.AddHeader("Content-Transfer-Encoding", "binary");
            }
    
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Length", contentLength.ToString());
    
            // Even though "Content-Disposition" should have an upper-case "d", as per http://www.ietf.org/rfc/rfc2183.txt
            // IE fails to recognize this if the "d" is upper-cased.
            Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
    
            var buffer = new byte[8192];
    
            while (Response.IsClientConnected)
            {
                var count = fStream.Read(buffer, 0, buffer.Length);
                if (count == 0)
                {
                    break;
                }
    
                Response.OutputStream.Write(buffer, 0, count);
                Response.Flush();
            }
        }
    
        Response.Close();
    }
    
    0 讨论(0)
  • 2021-01-18 00:02

    While this isn't directly applicable since you're using MySql but it's something to consider (might even be available for free by using sql server 2008 express). Sql Server 2008 offers Filestream support which lets you access files as if they are directly stored in the database but actually reside on a fileserver. Then information on the other posts can help you with getting it to the user.

    FILESTREAM Storage in SQL Server 2008

    0 讨论(0)
  • 2021-01-18 00:12

    This thread has my solution to keep memory usage down while users are downloading files. You probably want a bigger buffer than my sample uses, though.

    0 讨论(0)
提交回复
热议问题