简体   繁体   English

ASP.NET Web API 2中的带有AuthorizationFilterAttributes的条件或

[英]Conditional OR with AuthorizationFilterAttributes in ASP.NET Web API 2

I am writing a controller action that can be accessed by one of two groups of users; 我正在编写一个控制器操作,两组用户之一可以访问它。 each group has it's own implementation of AuthorizationFilterAttribute containing custom logic defining how the group be authorized. 每个组都有自己的AuthorizationFilterAttribute实现,其中包含定义如何授权组的自定义逻辑。 I want to be able to use a conditional OR to determine that at least one of the attributes authorization filter has been met. 我希望能够使用条件OR来确定已满足至少一个属性授权过滤器。

I was hoping that I would be able to do something like this: 我希望我能够做这样的事情:

public class ConfigController : ApiController
    {
        [AdminAuthorize || DealRoomAuthorizeAttribute]
        public IHttpActionResult GetBlah()
        {
            return Ok();
        }
    }

But no luck! 但是没有运气! Any ideas on how this can be acheived? 关于如何实现这一点的任何想法?

You cannot do the OR directly because of the way the authorization attributes work: they "cut" the pipeline if the authorization fails, so, if the first one fails it will stop the pipeline, and the other won't have the chance to be executed. 由于授权属性的工作方式,您不能直接执行“或”操作:如果授权失败,它们会“切断”管道,因此,如果第一个失败,它将停止管道,而另一个则没有机会执行。

You need to implement your own authorization attribute that makes the OR yourself. 您需要实现自己的授权属性,使您自己成为OR。 It's very easy to implement, because you simply have to reuse the logic of the existing attributes. 这非常容易实现,因为您只需要重用现有属性的逻辑即可。 In fact, you can inherit one of them, and override the derired methods, at least OnAuthorization , by reusing the exiting logic. 实际上,您可以继承它们之一,并通过重用现有逻辑来覆盖已废弃的方法,至少是OnAuthorization

More details: 更多细节:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM