小编Ces*_*sar的帖子

MVC 5 FileContentResult操作结果权限和重定向

我有一个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 …
Run Code Online (Sandbox Code Playgroud)

filecontentresult asp.net-mvc-5

2
推荐指数
1
解决办法
3465
查看次数

标签 统计

asp.net-mvc-5 ×1

filecontentresult ×1