- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
寻找防止恶意数据更改的想法:用户 A 操纵(编辑或删除)属于用户 B 的数据。由于我们是在客户端创建实体,因此我们需要将它们(或至少其中一些)分配给经过身份验证的用户。
例如:
var newItem = ds.createNewItem();
newItem.OwnerId(22); //this is the problem that I see.
newItem.Name("New Item");
newItem.Description("I just changed your item!");
... //and so on
ds.saveChanges();
假设我们知道在我们的 API 上调用 SaveChanges
的用户的身份,我们如何针对该用户验证我们的实体(新的或修改的)?
首先想到的是子类化 EFContextProvider
,重写 BeforeSaveEntity
并根据我们的用户身份检查实体 OwnerId
属性.例如:
if (entityInfo.Entity.GetType() == typeof(Item)
&& (entityInfo.EntityState == EntityState.Added
|| entityInfo.EntityState == EntityState.Modified)
&& ((Item)entityInfo.Entity).OwnerId != _currentUserId) {
return false
... //and so on
如果使用这种方法,在我们新的 EFContextProvider
类的构造函数中建立 _currentUserId
是否有意义?
解决这个问题的想法或更好的方法?
最佳答案
我认为您走在正确的轨道上。我自己也一直在思考这个问题,并且走了大致相同的道路。
假设您已经处理了身份验证并且有可用的 IPrincipal
。您也有一个自定义的 IIdentity
(称之为 AppIdentity
),您可以在其中存储经过身份验证的用户的 UserId
。
Web Api 的基础 ApiController
类通过其 User
属性使环境 IPrincipal
可用。我们将在您的自定义 Breeze Web Api Controller 中利用它,它可能像这样开始:
[Authorize][JsonFormatter, ODataActionFilter]public class BreezeApiController : ApiController{ private readonly AppContextProvider _context; public BreezeApiController() { // pass 'User' IPrincipal to the context ctor _context = new AppContextProvider(User); } ... // one of the Query action methods [HttpGet] public IQueryable<Foo> Foos() { return _context.Foos } ...
您的自定义 EFContextProvider
可能像这样开始:
public class AppContextProvider : EFContextProvider<AppDbContext>{ public AppContextProvider(IPrincipal user) { UserId = ((AppIdentity) user.Identity).UserId; } public int UserId { get; private set; } ...
现在您可能想要防止 UserB 的实体被 UserA 看到。因此,您的自定义 EFContextProvider
可以相应地进行过滤,而不是允许每个 Foo
出门。
public DbQuery Foos { get { // Here the 'Context' is your EF DbContext return (DbQuery) Context.Foos .Where(f => f.UserId == UserId); } }
回顾一下 Controller ,我们看到它的 Foos
GET 操作方法忽略了过滤器……这是应该的。我们希望我们的 Controller 轻便,并将业务逻辑移至自定义 EFContextProvider
及其助手。
最后,高度简化的通用 BeforeSaveEntity
可能如下所示:
private bool BeforeSaveEntity(EntityInfo info){ var entity = info.Entity; if (info.EntityState == EntityState.Added) { entity.UserId = UserId; return true; } return UserId == entity.UserId || throwCannotSaveEntityForThisUser();}...private bool throwCannotSaveEntityForThisUser(){ throw new SecurityException("Unauthorized user");}
请注意,服务器上的自定义上下文提供程序负责设置添加的实体的 UserId
。无论如何,我们不会相信客户会这样做。当然,它还负责验证修改和删除实体的 UserId
。
希望这对您有所帮助。 请记住,这只是草图。真正的交易会更加复杂,并被重构为助手。
关于breeze - 防止恶意数据更改的策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595745/
我正在考虑使用 Breeze js,并对其功能和随之而来的最佳实践有一些疑问。 服务器端元数据是否必须存在?如果我有一个非 EF WebApi Controller ,我是否仍然需要用 Provide
我们正在考虑使用breeze js来构建企业应用程序。 轻而易举的是,我们可以直接从客户端浏览器执行查询。这允许基于用户输入构造动态查询,而无需加载不必要的数据。我发现使用Breeze可以创建业务逻辑
我有一个从另一个表继承的表。当我运行该项目时,出现以下错误:无法获取未定义或空引用的属性“propertyref”...有什么想法吗? 最佳答案 编辑:从 v 1.3.1 Breeze 开始,现在支持
想知道是否有人知道扩展或配置 Breeze 以便服务器返回实体元数据中的附加信息的任何方法?我想使用这些附加数据来协助验证。 假设我有一个应用了一些数据注释的实体模型: public class Pe
我使用 Durandal/breeze 开发了一个 asp.net 解决方案。 这是我获取所有托运人的代码: var query = EntityQuery.from('Shippers')
我已经阅读了 Breeze 验证器信息,但不确定如何查看正在发生的实际错误。 错误:遇到客户端验证错误 - 有关更多详细信息,请参阅此对象上的实体错误集合。 我相信它在 entity.entityAs
我正在使用 Breeze 过滤客户端请求的数据。我的代码看起来有点像这样: 客户端 - 创建过滤谓词 var predicates = []; var criteriaPredicate = null
我在处理日期时遇到了一些问题。 我有一个带有日期字段的对象: public DateTime FechaInicio{get;设置; 此定义在数据库中生成以下字段: FechaInicio 日期时间不
我在处理日期时遇到了一些问题。 我有一个带有日期字段的对象: public DateTime FechaInicio{get;设置; 此定义在数据库中生成以下字段: FechaInicio 日期时间不
如果我使用微风加载部分实体: var query = EntityQuery.from('material') .select('Id, MaterialName, Mater
改进我关于如何使用获得的元数据在淘汰赛中创建验证规则的示例(http://stackoverflow.com/questions/13662446/knockout-validation-using-
我想更改 Breeze 的序列化程序,以便我的 javascript 对象是驼峰式的。在文档中它说这是可以做到的,但说这是有待解释的。 You can change the formatter's c
给定一个具有导航属性的实体,有没有办法判断空导航属性是否是因为子实体从未被加载,而不是集合已经加载但只是空的? 例如:我加载一个包含实体 A 列表的页面。每个实体 A 都有一个导航属性,它是实体 B
我使用 Entity Framework Code First + Durandal + Breeze 开发一个项目。 我有这些实体模型: public class Packing { [Ke
我正在尝试使用 breeze.js 保存一些实体。 Breeze 工作正常,它会根据需要保存所有更改。但是,我无法验证并确保授权是服务器端的。从我目前收集到的信息来看,我猜想做到这一点的唯一方法是检查
我们有一个轻而易举的客户解决方案,其中向父实体显示其子列表。我们对某些子实体进行硬删除。现在,当用户是执行删除操作的用户时,没有问题,但是当其他用户执行操作时,似乎没有办法使已经加载到缓存中的子项失效
我想用 Breeze.js 搜索文本 var qu = new breeze.EntityQuery("Projects") .where("Name", "s
从表面上看,这可能听起来像一个愚蠢的问题,但为什么 Hot Towel SPA Template包括 Breeze根本? 最近几天我一直在学习 Hot Towel 及其依赖项,据我所知,模板中没有任何
假设我想使用 Breeze 创建一个任务实体(我正在使用 EF),但是因为这是一个真实任务应用程序,所以该任务必须关联到当前登录的用户.任务具有需要在保存期间填充的 UserId 外键,这就是我遇到的
我正在尝试根据子实体的集合来过滤实体。这是我的实体(EF POCO): public class Customer { public int Id { get; set; } publ
我是一名优秀的程序员,十分优秀!