2

ASPMVC3Webサイトでファイルのアップロードがうまく機能しています。現在、ファイルは「ファイル」と呼ばれるWebサイト上のフォルダに保存されています。ユーザーは、任意のタイプのファイル(myphoto.jpg、mydocument.docxなど)をアップロードできます。

ユーザーがファイルをアップロードすると、ファイルに関する情報がSQLデータベースに保存され、誰がアップロードしたかなどがわかります。

私の質問:

  1. ファイルURL(例:/Files/myphoto.jpg)へのGETリクエストをインターセプトして、ユーザーがそのファイルを表示できるかどうかを確認するにはどうすればよいですか?(アプリケーションでの権利に基づく)?アクセスを許可する前にデータベースをチェックするルート制約を作成するというアイデアは好きではありません。
  2. 理想的には、ファイルをWebサイトのファイルの場所とは別の場所に保存したいのですが、Webサイトが要求からファイルとその場所を判別できる場所でありながら、要求された場所にあるかのようにファイルを提供します(正しいコンテンツタイプのヘッダーなど)。
4

2 に答える 2

5

ファイルをサーバー化するコントローラーを追加できます。

public class FileController : Controller
{

    private IFileStore _fileStore;

    public FileController(IFileStrore fileStore)
    {
        this._fileStore = fileStore; 
    }

    public ActionResult Index(string fileName)
    {
        // Do a database look up if the user has permission
        if (_fileStore.HasPermission(fileName, User))
        {
            // Flush the file content if the user has permission
            var myfile = _fileStore.GetFile(fileName);
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.FileName);
            Response.AddHeader("Content-Length", myfile.Length.ToString());

            Response.ContentType = myfile.Extension.ToLower();
            Response.WriteFile(myfile.FullName, false);
            Response.Flush();
            Response.Close();
        }
        else
        {
            // Return a Access Denied page
            return View("NoPermission");
        }
    }

}
于 2012-07-31T08:42:32.193 に答える
0

web.configで設定できます。

<!-- This section gives the authenticated user with myRole role access to all of the files that are stored in the images folder.  -->
<location path="images">
   <system.web>
     <authorization>
    <allow users =".\myRole" />
     </authorization>
   </system.web>
</location>

http://msdn.microsoft.com/en-us/library/8d82143t

于 2012-07-31T10:06:56.470 に答える