简体   繁体   English

OData WebAPI 2综合授权

[英]OData WebAPI 2 Complex Authorization

We have a Web Api 2 OData v3 service which we need to implement fairly complex authorization on. 我们有一个Web Api 2 OData v3服务,我们需要对其实施相当复杂的授权。 We are using Breeze within our client code and when an OData v4 version of Breeze is released we are upgrading our API to OData v4 so the solution needs to be able to work on both OData versions. 我们在客户端代码中使用Breeze,并且当Breeze的OData v4版本发布时,我们正在将API升级到OData v4,因此该解决方案需要能够在两个OData版本上都可以使用。

This diagram gives a very basic view of the sort of entity model we are working with (Sorry, not enough reputation points for an image): 此图提供了我们正在使用的实体模型的一种非常基本的视图(对不起,图像的信誉点不足):

 ServiceCompany
     |   
     |  
    /|\  
Manufacturer
     |   
     |  
    /|\    
    Site
     |   
     |  
    /|\  
  SiteArea  -------
     |            |
     |            |            
    /|\          /|\ 
 Equipment     Instrument
                  |
                  |
                 /|\
               Channel

A SiteArea has a property of “OperatingFunction” – this shows what stage of the manufacturing process occurs at this site area location. SiteArea具有“ OperatingFunction”的属性-这表明在此站点区域位置发生制造过程的哪个阶段。

A user could be 一个用户可能是

  • a person sitting at Manufacturer level and has access all of their Site data 坐在制造商级别并可以访问其所有站点数据的人
  • a person sitting at Manufacturer level and has access to some, but not all of their SiteAreas, and therefore only a subsection of their SiteArea data 坐在制造商级别的人员,可以访问其某些但不是全部的SiteArea,因此只能访问其SiteArea数据的一部分
  • a person sitting at ServiceCompany level who has access to some Manufacturers, and within that only some Sites within those Manufacturers, and maybe only certain OperatingFunctions of a SiteArea. 坐在ServiceCompany级别的人员,可以访问某些制造商,并且在其中只能访问这些制造商中的某些站点,并且可能仅可以访问SiteArea的某些操作功能。

A request will come in for Channel data and we need to be sure to only return or update (depending on the request type) the data that the individual is allowed to affect. 将会有一个针对Channel数据的请求,我们需要确保仅返回或更新(取决于请求类型)允许个人影响的数据。

Upon initial investigation the obvious choice for implementing this appeared to be QueryInterceptors and ChangeInterceptors, meaning we could add further filter preferences based upon the claims being sent in with the request. 初步调查后,实现此目的的明显选择是QueryInterceptors和ChangeInterceptors,这意味着我们可以根据与请求一起发送的声明添加其他过滤器首选项。 However it appears that Query/ChangeInterceptors are part of Wcf, not WebApi, and on top of that they are only part of the v1-3 Wcf OData implementation, there is nothing so far for OData v4. 但是,似乎Query / ChangeInterceptor是Wcf的一部分,而不是WebApi,并且最重要的是,它们仅是v1-3 Wcf OData实现的一部分,到目前为止,对于OData v4来说还没有任何东西。

We could, of course, write the filter code into each method, but that really seems like it would be a nasty way to implement something that is essentially a cross cutting concern. 当然,我们可以将过滤器代码写入每个方法中,但实际上这似乎是实现本质上涉及交叉问题的讨厌方法。

We have looked at the EF6 interceptors but have arrived at the conclusion that they're too far down the stack, ideally we don't want to be dealing with the SQL command code itself. 我们已经研究了EF6拦截器,但得出的结论是它们距离堆栈太远了,理想情况下,我们不想处理SQL命令代码本身。

We have briefly considered whether we should use a role/task based authorisation pattern and have come to a solid conclusion that that would not work for us as it would be too constricting for future developments and wouldn't work in our scaling plans. 我们已经简短地考虑了是否应该使用基于角色/任务的授权模式,并得出了一个可靠的结论,即这对我们不起作用,因为这对于将来的开发会太有局限性,并且在我们的扩展计划中将不起作用。

Essentially we have come to the conclusion that we need to implement our own QueryInterceptorAttribute but thought it worth asking if we have missed something before trying to reinvent the wheel. 从本质上讲,我们得出的结论是,我们需要实现自己的QueryInterceptorAttribute,但认为值得一提的是,在尝试重新发明轮子之前是否错过了某些内容。

Thanks. 谢谢。

Edit : I forgot to mention that another option may be to look at using the Decorator pattern, we're using Unity and with that could add the functionality we require : Unity Interception 编辑:我忘了提到另一个选择,可能是使用Decorator模式,我们正在使用Unity,并且可以添加我们需要的功能: Unity Interception

I went down the QueryInterceptor path and it's nothing but a bottomless abyss of abstraction. 我走在QueryInterceptor道路上,无非是抽象的无底深渊。 I could not find a hook at any point where things were a) decipherable and/or b) not read-only. 我在任何情况下都无法找到一个钩子:a)可解密的和/或b)不是只读的。

Have a look at this article by Dominick Baier of Thinktecture. 看看Thinktecture的Dominick Baier的这篇文章。
http://leastprivilege.com/2014/06/24/resourceaction-based-authorization-for-owin-and-mvc-and-web-api/ http://leastprivilege.com/2014/06/24/resourceaction-based-authorization-for-owin-and-mvc-and-web-api/

I am using this claims-based resource/action authorization model to great effect. 我正在使用这种基于声明的资源/操作授权模型,以取得巨大的效果。 I also suggest viewing Dominick's tutorials on Pluralsight. 我还建议查看Dominick的Pluralsight教程。 They helped me a great deal. 他们给了我很大帮助。

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

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