首页 文章

MVC5授权文件下载MVC5

提问于
浏览
0

我正在寻找在我的MVC5应用程序中为已经获得登录授权的用户提供文件下载服务 . 我计划使用如下的物理路径结构

App_Data/Files/{ItemName}/{FileName}.{FileExtension} (大多数情况下FileExtension都是Zip)

财富

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

ApprovedToDownload

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

当person获得授权时,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);
           }

考虑到安全性,我最好的选择是什么?换句话说,任何人都可以建议我,如果我将面临任何问题,并可以指出我的最佳实践方向 .

1 回答

  • 0

    ActionFilters或RouteConstraints将完成我认为的技巧 .

相关问题