I'm reading from several resources (books and SO answers) about authorization in WebApi.

假设我想添加只允许某些用户访问的自定义属性:

Case #1

我见过overridingOnAuthorization这种方法,如果出现问题,它会设置响应

public class AllowOnlyCertainUsers : AuthorizeAttribute
{
 public override void OnAuthorization(HttpActionContext actionContext)
  {
   if ( /*check if user OK or not*/)
   {
     actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
   }
  }
}

Case #2

但我也见过这个类似的例子,它也超过了OnAuthorization,但呼叫到了base:

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

    // If not authorized at all, don't bother

    if (actionContext.Response == null)  
     {
      //...
     }
}

Then, you check if the 100 is set or not. If it’s not set, it means that the request is authorized and the user is ok

Case #3

但我也见过这种超越IsAuthorized的方法:

public class AllowOnlyCertainUsers : AuthorizeAttribute
{
 protected override bool IsAuthorized(HttpActionContext context)
  {
   if ( /*check if user OK or not*/)
   {
    return true;// or false
   }
  }
}

Case #4

然后我看到了一个类似的例子,不过是呼叫基地.未授权(上下文):

protected override bool IsAuthorized(HttpActionContext context)
{
 if (something1 && something2 && base.IsAuthorized(context)) //??
 return true;
 return false;
}

One more thing

最后多米尼克说here:

You shouldn't override OnAuthorization - because you would be missing [AllowAnonymous] handling.

Questions

  • 1) 我应该使用哪些方法:IsAuthorized还是OnAuthorization?(或何时使用哪个)

  • 2)何时拨打base.IsAuthorized orbase.OnAuthorization`?

  • 3)他们是这样建造的吗?如果响应为空,则一切正常?( case 2)

注意事项

请注意,我只使用了(并且希望使用)从AuthorizationFilterAttribute继承的AuthorizeAttribute

为什么?

因为我在第一阶段:http://www.asp.net/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api

在此处输入图像描述

不管怎样,我是通过扩展授权属性来询问的.

推荐答案

我应该使用哪些方法:IsAuthorized还是OnAuthorized?(或

如果授权逻辑不依赖于已建立的身份和角色,您将扩展AuthorizationFilterAttribute.对于与用户相关的授权,您将扩展并使用AuthorizeAttribute.对于前一种情况,您将覆盖OnAuthorization.对于后一种情况,您将覆盖IsAuthorized.正如您可以从这些属性的源代码中看到的那样,如果您从AuthorizationFilterAttribute派生,OnAuthorization将被标记为虚拟,以便您进行覆盖.另一方面,IsAuthorized方法在AuthorizeAttribute中被标记为虚拟的.我相信这是指向预期用途的一个很好的指针.

我什么时候给基地打电话.未授权或未授权.授权?

这个问题的答案在于OO通常是如何工作的.如果重写一个方法,则可以完全提供一个新的实现,也可以借助父级提供的实现来增强行为.例如,以IsAuthorized(HttpActionContext)为例.基类行为是根据过滤器中指定的内容和建立的标识判断用户/角色.比如说,你想做所有这些,但除此之外,你想判断其他东西,可能是基于请求头或其他东西.在这种情况下,可以提供这样的覆盖.

protected override bool IsAuthorized(HttpActionContext actionContext)
{
    bool isAuthroized = base.IsAuthorized(actionContext);
    // Here you look at the header and do your additional stuff based on actionContext
    // and store the result in isRequestHeaderOk
    // Then, you can combine the results
    // return isAuthorized && isRequestHeaderOk;
}

对不起,我不明白你的问题3.顺便说一句,授权过滤器已经存在很长一段时间了,人们在各种事情上都使用它,有时也不正确.

还有一件事.最后有个家伙说:你

说这话的人是门禁之神多米尼克.显然,这将是正确的.如果您查看OnAuthorization的实现(复制如下),

public override void OnAuthorization(HttpActionContext actionContext)
{
    if (actionContext == null)
    {
        throw Error.ArgumentNull("actionContext");
    }

    if (SkipAuthorization(actionContext))
    {
        return;
    }

    if (!IsAuthorized(actionContext))
    {
        HandleUnauthorizedRequest(actionContext);
    }
}

SkipAuthorization的调用是确保应用AllowAnonymous个过滤器的部分,即跳过授权.如果你覆盖了这个方法,你就会失go 这种行为.实际上,如果您决定基于用户/角色进行授权,那么此时您将决定从AuthorizeAttribute派生.此时留给您的唯一正确选项是覆盖IsAuthorized,而不是已经覆盖的OnAuthorization,尽管从技术上讲,这两种方法都是可以做到的.

在ASP.NET Web API,还有另一个过滤器称为身份验证过滤器.这个 idea 是,您使用它进行身份验证,并使用授权过滤器进行授权,正如名称所示.然而,有很多例子表明这一界限是模糊的.许多authroization过滤器示例将执行某种身份验证.不管怎样,如果你有时间想了解更多,看看这个MSDN article.免责声明:这是我写的.

Asp.net相关问答推荐

如何在ASP.NET中执行具有快速端点的GET请求?

如何在 C#/MVC 4 中的 Html.TextBoxFor 中输入占位符文本

我们可以在一个网页中使用多个表单吗?

*不*使用 asp.net 会员提供程序是个坏主意吗?

如何从 dll 文件中提取类的源代码?

Window.Open 使用 PDF 流而不是 PDF 位置

如何在 ASP.NET 中使用时区?

Asp.Net 会话在 ashx 文件中为空

有没有办法在外部 javascript 文件中使用<%= someObject.ClientID %>?

对布尔查询字符串参数使用true或1

如何使图像的一部分成为可点击的链接

无法加载文件或程序集'System.Web.WebPages.Razor,版本 = 3.0.0.0

MvcBuildViews true 与 ASP.NET MVC 2 中的实体框架

HttpContext.Current.Cache.Insert 和 HttpContext.Current.Cache.Add 有什么区别

如何使用自动生成的列隐藏 ASP.NET GridView 中的列?

错误:无法在 Web 服务器上开始调试... ASP.NET 4.0

如何从后面的 ASP.NET 代码访问 HTML 表单输入

如何删除asp.net中的特定会话?

.NET 4.0 中的自定义 MembershipProvider

ASP.NET Excel 导出编码问题