使用MVC4在运行时处理文件权限 [英] Handling file permissions on runtime using MVC4

查看:105
本文介绍了使用MVC4在运行时处理文件权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用MVC4.

I'm currently using MVC4.

  • 我的项目中有一个文件夹,其中包含文件(图像,doc和pdf 文件).
  • 我有一个数据库,其中包含用户列表(.net成员身份 guid)有权访问每个文件.
  • I have a folder in my project that has files (images, doc and pdf files).
  • I have a database with a list of users (.net membership guids) that have permission to access each of the files.

我目前正在研究什么,我正在向您提出建议,因为我从未在MVC上做过:

What I'm currently researching and I'm asking you advice, as I never did it on MVC:

关于如何捕获文件的http请求,然后在运行时决定用户是否具有访问权限的任何提示? 是否有任何一种HTTP处理程序可以让我在MVC上执行此操作? 还有其他想法或提示吗?

Any tip regarding how to catch the http request of the file and then decide on runtime if the user has permissions to acces or not? Is there any kind of http handler that let me do that on MVC? Any other idea or tip?

预先感谢!..

即插即用

推荐答案

我将创建一个新的FileAuthorizeAttribute并装饰一个新的控制器方法.

I would create a new FileAuthorizeAttribute and decorate a new controller method.

public class FileAuthorizeAttribute: AuthorizeAttribute
{
    protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        if (base.IsAuthorized(actionContext))
        {
            // check if guid is in your database
        }

        return false;
    }

    public override void OnAuthorization(HttpActionContext actionContext)
    {
        base.OnAuthorization(actionContext);
    }
}

public class FileController : Controller
{
    [FileAuthorize]
    public FileResult Load(string fileName)
    {
        //return File(fileName, contentType);
    }
}

这篇关于使用MVC4在运行时处理文件权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆