- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 MVC 5 应用程序,允许用户下载存储在数据库中的文件。我正在使用 FileContentResult 操作方法来执行此操作。
我可以在整个应用程序中限制对此方法的访问,但聪明的用户可以找出操作 URL 并将类似这样的内容 (localhost:50000/Home/FileDownload?id=13) 粘贴到他们的浏览器中并有权下载只需更改参数即可任何文件。
我想限制用户这样做。仅允许管理员角色和具有只能通过数据库调用确定的特定权限的用户下载文件。
我正在寻找的是,如果用户使用 URL 下载文件并且没有适当的权限,我想用消息重定向用户。
我想做类似下面的代码或类似的事情,但出现以下错误:无法将类型“System.Web.Mvc.RedirectToRouteResult”隐式转换为“System.Web.Mvc.FileContentResult”
我知道我不能在这里使用 return RedirectToAction("Index") ,只是寻找一些有关如何处理此问题的想法。
public FileContentResult FileDownload(int id)
{
//Check user has file download permission
bool UserHasPermission = Convert.ToInt32(context.CheckUserHasFileDownloadPermission(id)) == 0 ? false : true;
if (User.IsInRole("Administrator") || UserHasPermission)
{
//declare byte array to get file content from database and string to store file name
byte[] fileData;
string fileName;
//create object of LINQ to SQL class
//using LINQ expression to get record from database for given id value
var record = from p in context.UploadedFiles
where p.Id == id
select p;
//only one record will be returned from database as expression uses condtion on primary field
//so get first record from returned values and retrive file content (binary) and filename
fileData = (byte[])record.First().FileData.ToArray();
fileName = record.First().FileName;
//return file and provide byte file content and file name
return File(fileData, "text", fileName);
}
else
{
TempData["Message"] = "Record not found";
return RedirectToAction("Index");
}
}
最佳答案
由于 FileContentResult
和 RedirectToRouteResult
都是从 ActionResult
继承的,因此只需使用 ActionResult
而不是 FileContentResult
作为操作的返回类型:
public ActionResult FileDownload(int id)
{
if(IsUserCanDownloadFile()) // your logic here
{
// fetch the file
return File(fileData, "text", fileName);
}
return RedirectToAction("Index");
}
或者,如果您更喜欢属性,您可以编写自己的授权属性来检查权限:
public class FileAccessAttribute : AuthorizeAttribute
{
private string _keyName;
public FileAccessAttribute (string keyName)
{
_keyName = keyName;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// imagine you have a service which could check the Permission
return base.AuthorizeCore(httpContext)
|| (this.ContainsKey
&& _permissionService.CanDownload(httpContext.User.Identity.GetUserId(),
int.Parse(this.KeyValue.ToString()));
}
private bool ContainsKey
{
get
{
// for simplicity I just check route data
// in real world you might need to check query string too
return ((MvcHandler)HttpContext.Current.Handler).RequestContext
.RouteData.Values.ContainsKey(_keyName);
}
}
private object KeyValue
{
get
{
return ((MvcHandler)HttpContext.Current.Handler)
.RequestContext.RouteData.Values[_keyName];
}
}
}
现在您可以在操作上装饰自定义属性:
[FileAccess("id", Roles ="Administrator")]
public FileContentResult FileDownload(int id)
{
// fetch the file
return File(fileData, "text", fileName);
}
关于asp.net-mvc-5 - MVC 5 FileContentResult 操作结果权限和重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35115595/
我正在使用 FileContentResult 方法下载生成的 PDF 文件。运行这段代码后 public FileContentResult genPDF(PDFFileData m) {
我正在从对操作方法的调用中返回图像 (png),我想停止浏览器缓存图像。 return File(reply, "image/png", "{0}_Graph".FormatWith(ciName))
我有一个 Controller 操作,它根据容器引用名称(即 blob 中文件的完整路径名)从 azure blob 下载文件。代码看起来像这样: public FileContentResult G
我有 API 端点,当用户被授权访问文件时,它返回 FileContentResult。当用户没有访问权限时,我想返回 Unauthorized/401 错误。 [HttpGet] [Authoriz
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个将数据导出到 CSV 文件的方法。 public FileContentResult Index(SearchModel search) { ... if (search
我想从 FileContentResult 结果方法返回一个“默认图像”,而不是 null。基本上,我在整个项目的多个 View 中调用了以下方法。但问题是,当检索方法没有 Image 时,它返回
我正在使用 fileContentResult 将文件呈现到浏览器。它工作得很好,只是当文件名包含国际字符时它会抛出异常。我记得在某处读到过此功能不支持国际字符,但我确信在应用程序需要在美国以外的国家
我有一个从字节数组中获取的 FileContentResult,我想将其转换为 FileStreamResult。这可能吗?如果是这样,如何? 最佳答案 首先,我对 MVC 了解不多,但听起来您不需要
我正在编辑一些代码,有一种方法返回 FileContentResult 类型。我从服务中获取流,所以对我来说,将返回类型更改为 FileStreamResult 会更方便。 我应该将流转换为数组以返回
在我的 MVC 项目中,我的 View 上生成了一个图表,如下所示 它调用一个返回 FileContentResult 的 Controller Function GetChart() As Fil
我的 Controller 操作之一将文件返回给用户。我希望向用户显示下载(打开/保存)对话框,无论文件类型如何。当文件类型为 .doc、.docx、.xlsx 等时,此方法工作正常,但当文件为 .t
我正在尝试使用 Rotativa用于在 Web 服务器磁盘上永久存储(而不是显示)发票副本的组件。两个问题: 为什么我需要指定 Controller 操作? (“索引”,在此案例) 如何将 FileC
目前,我有以下操作从我的数据库返回文件(图像、PDF 等): // // GET: /FileManager/GetFile/ID [OutputCache(Duration
我有这样的 MVC 路线: public FileContentResult GetMedia(string media_md5) { // get media from DB here
在生成的报告列表中,有一个下载它们的链接。这些报告的内容存储在数据库中。当我执行下载报告时,使用以下代码: return new FileContentResult (report.FileConte
我当前正在尝试查看以 BLOB 形式存储在数据库中的图像,为此我创建了一个 FileContentResult 类型的 Controller 操作,如下所示: public FileContentRe
我正在从 MVC 3 网站生成 CSV,并使用 FileContentResult 将其传递给用户。这效果很好,但生成 csv 需要 30 秒,因此在提示用户保存之前需要 30 秒。 publ
在常规 MVC Controller 中,我们可以使用 FileContentResult 输出 pdf。 public FileContentResult Test(TestViewModel vm
在常规 MVC Controller 中,我们可以使用 FileContentResult 输出 pdf。 public FileContentResult Test(TestViewModel vm
我是一名优秀的程序员,十分优秀!