2016-08-16 62 views
0

我正在查看將MVC5應用程序中的文件下載文件下載到已登錄授權的用戶。 我打算使用一個物理路徑結構如下圖MVC5授權文件下載MVC5

App_Data/Files/{ItemName}/{FileName}.{FileExtension}(FileExtension在大多數情況下,郵編)

資產

{**AssetId**, ItemName, FileName, FileExtension, FileLive} 

ApprovedToDownload

{**Id**, **userId**, **AssetId**, GUID, ExpireDate, StartDate} 

當人得到授權userId在ApprovedToDownload表中與上面鏈接,併爲訪問該文件創建動態GUID。 的人會下載文件,URL像

/my-assets/download/GUID 

控制器

[Authorize] 
     [Route("my-assets/download/{id}",Name="mydload")] 
     [HttpGet] 
     public FileContentResult Download(Guid? id) 
     { 
      int userId = User.Identity.GetUserId<int>(); 
      if (id == null) { 
       throw new HttpException(404, "Not found"); 
      } 
      var fzip = db.ApprovedToDownloads.SingleOrDefault(a => a.GUID == id && a.UserId == userId && a.FileLive && a.ExpireDate <= DateTime.Today); 

      string fid = id.Value.ToString(); 
      string fldr=fzip.Asset.ItemName; 
      string fnom = fzip.Asset.FileName; 
      string fext = fzip.Asset.FileExtension; 
      // Read bytes from disk 
      byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/Files/"+fldr+"/"+fnom+"."+fext)); 

      return File(fileBytes, "application/zip", fid+"."+fext); 
      } 

哪些考慮到安全問題,實施這個我最好的選擇嗎?換句話說,任何人都可以建議我,如果我將面臨任何問題,並可以指向最佳實踐方向。

回答

0

ActionFilters或RouteConstraints將做我認爲的詭計。