所有实体上的QueryInterceptor

时间:2014-07-29 17:59:14

标签: c# entity-framework wcf wcf-data-services queryinterceptor

是否可以在所有进程中的WCF数据服务中创建自定义QueryInterceptor而不是仅有一个?这是一个标准的QueryInterceptor:

[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("*")]
public Expression<Func<Object, bool>> OnQueryProducts()
{
    var user = HttpContext.Current.User;
    if (user.IsInRole("Administrator"))
        return (Object p) => true;
    else
        return (Object p) => false;
}

有没有办法或者我必须为我的所有实体整合一个接受器?

1 个答案:

答案 0 :(得分:2)

不幸的是,您不能将通配符与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);
}