C# 所有实体上的QueryInterceptor
是否可以在WCF数据服务中对所有实体而不是仅对一个实体进行自定义?这是一个标准的查询接收器:C# 所有实体上的QueryInterceptor,c#,entity-framework,wcf,wcf-data-services,queryinterceptor,C#,Entity Framework,Wcf,Wcf Data Services,Queryinterceptor,是否可以在WCF数据服务中对所有实体而不是仅对一个实体进行自定义?这是一个标准的查询接收器: [QueryInterceptor("Products")] public Expression<Func<Product, bool>> OnQueryProducts() { var user = HttpContext.Current.User; if (user.IsInRole("Administrator")) return (Prod
[QueryInterceptor("Products")]
public Expression<Func<Product, bool>> OnQueryProducts()
{
var user = HttpContext.Current.User;
if (user.IsInRole("Administrator"))
return (Product p) => true;
else
return (Product p) => false;
}
[QueryInterceptor(“产品”)]
QueryProducts()上的公共表达式
{
var user=HttpContext.Current.user;
if(user.IsInRole(“管理员”))
返回(产品p)=>真;
其他的
退货(产品p)=>假;
}
我喜欢这样做:
[QueryInterceptor("*")]
public Expression<Func<Object, bool>> OnQueryProducts()
{
var user = HttpContext.Current.User;
if (user.IsInRole("Administrator"))
return (Object p) => true;
else
return (Object p) => false;
}
[QueryInterceptor(“*”)]
QueryProducts()上的公共表达式
{
var user=HttpContext.Current.user;
if(user.IsInRole(“管理员”))
返回(对象p)=>true;
其他的
返回(对象p)=>false;
}
是否有任何方法或我必须为我的所有实体集成一个接收器?不幸的是,您不能将通配符与QueryInterceptor一起使用,但是您可以通过覆盖DataService的OnStartProcessingRequest方法并检查那里的用户角色来获得与示例中相同的结果
protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
var user = HttpContext.Current.User;
// Only Administrator users are allowed access
if (!user.IsInRole("Administrator"))
{
// Any other role throws a security exception
throw new SecurityException("You do not have permission to access this Service");
}
base.OnStartProcessingRequest(args);
}
不幸的是,我需要更多的逻辑(租户处理)。但是我现在为每个实体使用一个查询拦截器。正如你所说,这在目前是不可能的。所以我接受了你的答案。这是可能的,但你需要创建自己的拦截器逻辑,与Web API一起工作。正在覆盖默认行为…现在没有时间这么做。。。