WCF数据服务oData从QueryInterceptor抛出异常

时间:2013-12-17 13:56:37

标签: c# wcf odata

我有一种情况,即允许特定角色的用户查看oData Feed的全部内容,而允许没有该角色的用户查看“他们的”记录。

该模型包含UserId字段。注意:这适用于使用内置AD角色提供程序的内部Intranet站点。不具有特定角色的用户可以查看其用户ID与UserId字段的值匹配的任何记录。如果没有特定角色的个人试图访问那些值不匹配的记录,我想抛出一个Not Authorized异常。

最终,似乎QueryInterceptor返回的表达式稍后会转入SQL并用于过滤结果。这意味着throw语句在表达式中没有意义。

还有其他方法可以做我想要的吗?

我的一个查询拦截器示例:

[QueryInterceptor("Notifications")]
public Expression<Func<Notification, bool>> OnQueryNotifications()
{
    var user = HttpContext.Current.User;
    var userId = user.Identity.UserId(); //Extension method

    // returns boolean if the user is in any of the roles passed in the array
    var allowed = SiteSecurity.Allowed(user, new string[] { "Administrator", "Technician" });

    if (!allowed)
        // Here is where I need to test if the user is requesting any specific 
        // records, and if they are allowed to view those records.
        return q => q.UserId.Equals(userId, StringComparison.OrdinalIgnoreCase);
    else
        return q => true;
}

1 个答案:

答案 0 :(得分:0)

您可以通过仅返回他们可以访问的记录来排除记录(正如您在上面所做的那样)。

如果用户试图获取他们无法访问的数据,您也可以返回403禁止状态代码。

相关问题