Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 所有实体上的QueryInterceptor_C#_Entity Framework_Wcf_Wcf Data Services_Queryinterceptor - Fatal编程技术网

C# 所有实体上的QueryInterceptor

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

是否可以在WCF数据服务中对所有实体而不是仅对一个实体进行自定义?这是一个标准的查询接收器:

[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一起工作。正在覆盖默认行为…现在没有时间这么做。。。