实体框架的行级安全 [英] Row Level Security with Entity Framework

查看:195
本文介绍了实体框架的行级安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图考虑如何使用实体框架实现行级安全。这个想法是有一个数据库不可知意味着提供方法来限制来自ObjectContext的行。

I've been trying to consider how Row Level Security could be implemented with the Entity Framework. The idea is to have a database agnostic means that would offer methods to restrict the rows coming from the ObjectContext.

我的一些初始想法涉及修改创建的部分类EDMGEN工具,并提供了一些有限的支持。用户仍然能够通过使用自己的eSQL语句和QueryObject来解决这个解决方案。

Some of my inital ideas have involved modifying the partial classes created by the EDMGEN tool and that has offered some limited support. Users are still able to get around this solution by using their own eSQL statements and a QueryObject.

我一直在寻找一个存在于数据库提供程序之上的全面解决方案,以便它保持不可知。

I've been looking for a comprehensive solution that would exist above the database providers so that it would remain agnostic.

推荐答案

当然可以。重要的是要阻止对对象上下文的直接访问(阻止用户构建自己的ObjectQuery),而是给客户端一个更窄的网关,在其中访问和改变实体。我们使用实体存储库模式进行操作。您可以找到示例实现这个模式的实体框架在这篇博文。同样,关键是阻止对对象上下文的访问。注意对象上下文类是部分的。因此,您应该能够防止未授权实例化它的方法,即在您的版本库程序集外部。

Sure you can do it. The important thing to do is to block direct access to the object context (preventing users from building their own ObjectQuery), and instead give the client a narrower gateway within which to access and mutate entities. We do it with the Entity Repository pattern. You can find an example implementation of this pattern for the entity framework in this blog post. Again, the key is blocking access to the object context. Note that the object context class is partial. So you should be able to prevent "unauthorized" means of instantiating it, namely, outside of your repository assembly.

但是,有一些细微之处要考虑。如果通过存储库模式在某个实体类型上实现行级视图安全性,则必须考虑客户端可以访问相同实体的其他方法。例如,通过导航关系。您可能需要将其中一些关系设为私有,这可以在模型中进行。您还可以选择指定自定义查询或用于加载/保存实体的存储过程。存储过程往往是DB服务器特定的,但SQL可以以通用方式编写。

However, there are subtleties to consider. If you implement row-level view security on a certain entity type via the repository pattern, then you must consider other means by which a client could access the same entities. For example, via navigational relationships. You may need to make some of those relationships private, which you can do in your model. You also have the option of specifying a custom query or stored procedure for loading/saving entities. Stored procedures tend to be DB server specific, but SQL can be written in a generic manner.

虽然我不同意这不能用Entity Framework完成,我同意在DB服务器上的意见,因为你应该实现防御深度

While I don't agree that this cannot be done with the Entity Framework, I do agree with the "do it on the DB server" comments insofar as you should implement defense in depth.

这篇关于实体框架的行级安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆