Handling file permissions on runtime using MVC4

余生颓废 提交于 2019-12-24 08:57:40

问题


I'm currently using MVC4.

  • 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.

What I'm currently researching and I'm asking you advice, as I never did it on 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?

Thanks in advance!..

PnP


回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/13367037/handling-file-permissions-on-runtime-using-mvc4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!