当用户只能访问父对象时,如何构建子对象的安全性
我有一个类似于以下内容的数据库结构:
User
----
Id
Name
UserCustomerLink
----------------
UserId
CustomerId
Customer
--------
Id
Name
Address
-------
Id
CustomerId
Address1
Invoice
-------
Id
AddressId
Number
这是一个 asp.net mvc 站点,因此用户可以访问像 http://localhost/invoice/details/1
这样的地址,该地址将返回发票id 为 1。
用户需要登录并分配给发票(通过地址)所属的客户(通过 UserCustomerLink 表)。
我的问题是我应该在哪里检查用户是否可以查看发票?
我应该检查发票是否存在并且用户可以查看它然后从数据库返回它,或者我应该从数据库检索该项目然后检查?
我担心为查找此信息而执行的数据库查询的数量,并且我正在寻找一种有效的方法。这是结构的简单视图,一些子属性的深度超过 3。
在站点注释中,我首先使用实体框架代码,因此如果有一种方法可以创建在发票对象中包含客户 ID 的映射,则可能可以解决此问题。
I have a database structure something similar to the following:
User
----
Id
Name
UserCustomerLink
----------------
UserId
CustomerId
Customer
--------
Id
Name
Address
-------
Id
CustomerId
Address1
Invoice
-------
Id
AddressId
Number
This a asp.net mvc site so a user can go to an address like http://localhost/invoice/details/1
which will return the invoice with an id of 1.
A user needs to be logged in and be assigned to the customer (via the UserCustomerLink table) that the invoice (via the address) belongs to.
My question is where should I be doing the check that the user can view the invoice?
Should I check the invoice exists and the user can view it and then return it from the database or should I retrieve the item from the database and then check?
I'm concerned about the number of database queries that will be executed to find this information and I'm looking for an efficient method. This a simplistic view of the structure and some of the child properties go more than 3 deep.
On a site note I'm using Entity Framework code first so if there's a way to create a mapping that includes the Customer Id in the Invoice object this could potential get around this issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有理由两次访问数据库。您可以使用联接并进行一项选择。您的所有表都有链接记录的外键。当查询返回时,您检查用户是否链接到该发票。你可以使用 LINQ 做同样的事情
There is no reason to make two trips to the database. You can use joins and do one select. All your tables have foreign keys that link the records. When the query returns you check that the User is linked to that invoice. You could do this same thing with LINQ
查看授权属性。您可以将您的用户放入角色,然后让框架为您进行检查。
Look into the Authorize attribute. You could put your users into Roles and then let the framework check for you.