Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 WebAPI控制器中的授权代码和授权属性_Asp.net Mvc 3_Web Applications_Asp.net Web Api - Fatal编程技术网

Asp.net mvc 3 WebAPI控制器中的授权代码和授权属性

Asp.net mvc 3 WebAPI控制器中的授权代码和授权属性,asp.net-mvc-3,web-applications,asp.net-web-api,Asp.net Mvc 3,Web Applications,Asp.net Web Api,我使用以下方法检索文件夹: [HttpGet] [Authorize] public HttpResponseMessage GetFolder(int id) { FolderDto folder = _service.GetFolder(id); if (folder == null) { return Request.CreateResponse(HttpStatusCode.NotFo

我使用以下方法检索文件夹:

    [HttpGet]
    [Authorize]
    public HttpResponseMessage GetFolder(int id)
    {
        FolderDto folder = _service.GetFolder(id);
        if (folder == null)
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        return Request.CreateResponse(HttpStatusCode.Found, folder);
    }
我有授权工作-如果用户登录并使用有效cookie传递请求,操作将继续。但是我只想允许用户获取属于他的文件夹,也就是说,那些在成员表中用户ID为他的用户ID的文件夹

我应该为我的每一个WebAPI控制器方法在控制器中执行此检查,还是这里有一个我还没有了解的标准方法。我的直觉告诉我要这样做:

    [HttpGet]
    [Authorize]
    public HttpResponseMessage GetFolder(int id)
    {
        FolderDto folder = _service.GetFolder(id);
        if (folder == null || folder.UserId != <userId>) // Or just add another check for HTTPUnauthorized
        {
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

        return Request.CreateResponse(HttpStatusCode.Found, folder);
    }
[HttpGet]
[授权]
公共HttpResponseMessage GetFolder(int id)
{
FolderDto folder=\u service.GetFolder(id);
如果(folder==null | | folder.UserId!=)//或只是添加另一个HTTPUnauthorized检查
{
返回请求.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.Found,文件夹);
}

想法?

如果您只需要在执行一个操作期间执行此授权,那么我只需将该代码放入该操作代码中。但是如果您有多个操作,那么我可能会通过扩展
AuthorizeAttribute
并在该筛选器中移动文件夹授权代码来创建授权筛选器。

您显示的是用户没有访问权限的文件夹,或者这是一种边缘情况,您只想确保他们没有访问权限?我想确保用户无法访问不是他们的文件夹。。。但我不认为这是边缘案件。