WCF Dataservice - 在返回结果之前修改对象?

发布于 2024-12-04 06:20:22 字数 245 浏览 0 评论 0原文

我正在使用 WCF 数据服务,并且有一些字段/属性需要在发送回客户端之前“空白”(将值设置为空字符串或 null)。

例如:用户表有密码列,我不想将值传递给客户端。这是一个示例,应用程序中还有其他此类列,出于安全/隐私原因应排除该值。

很抱歉提出这样一个基本问题,我是 WCF 数据服务的新手,尚未找到任何有希望的线索。我尝试过 QueryInterceptors 但没有成功。

有人能指出我正确的方向吗?

谢谢

I am using WCF data services and I have a few fields/properties that I want to "blank out" (set value to empty string or null) before sending back to client.

For example: User table has password column which I do not want to pass the value to the client. This is one example, there are other such columns in the app that the value should be excluded for security/privacy reasons.

Sorry for such a basic question, I'm new to WCF dataservices and have not found any promising leads yet. I've tried QueryInterceptors but no luck.

Can someone point me in the right direction?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-12-11 06:20:22

IMO 这超出了 WCF 数据服务的范围。 WCF 数据服务旨在获取您的实体模型并根据访问规则将其公开。如果您的实体公开了一些属性,并且该实体公开了,则其属性只是公共的。适用于简单的CRUD场景或者只读场景。

QueryInterceptor 不会帮助您,因为它可用于数据驱动的授权 - 这意味着 QueryInterceptor 可以添加一些附加条件来过滤当前用户不允许查看的记录 =它将过滤掉整个记录,但不会修改过滤结果。

没有空字段的钩子,因为这是一个不好的方法。如果您不想公开某些字段,那么它们根本不应该成为公开实体的一部分。您可以使用 QueryView 创建仅公开公共字段的第二个只读实体在您的 EDMX 文件中。接下来,您需要修改 DataServiceConfiguration 中的访问规则。您必须删除对初始User 实体集的访问规则,并向该新实体集添加读取访问规则。

如果您需要控制每个用户的访问规则,则必须使用某种身份验证 在您的服务中,您必须在 InitializeService 方法中处理它(除非 DataServiceConfiguration 在其他地方可用)。类似于:

public static void InitializeService(DataServiceConfiguration config)
{
    var context = ServiceSecurityContext.Current;
    if (context != null && context.PrimaryIdentity != null)
    {
        var userName = context.PrimaryIdentity.Name;
        if (SomeMethodToValidateUserPermissions(userName)
        {
            config.SetEntitySetAccessRule("Users", EntitySetRights.AllRead);
        }
    }

    config.SetEntitySetAccessRule("TrimmedUsers", EntitySetRights.AllRead);
} 

通过更深入地研究 WCF,可以使用其他方法来限制对某些资源的访问,但这一种是最简单的。

IMO this is out of scope of WCF Data Services. WCF Data Services are meant to take your entity model and expose it as is based on access rules. If your entity exposes some properties and that entity is exposed its properties are simply public. It is for simple CRUD scenarios or read-only scenarios.

QueryInterceptor will not help you because it can be used for data driven authorization - it means that QueryInterceptor can add some additional condition to filter records which current user is not permitted to see = it will filter out whole records but it will not modify filtered result.

There is no hook to null fields because that is a bad approach. If you don't want expose some fields they should not be part of exposed entity at all. You can create second read-only entity exposing only public fields by using QueryView in your EDMX file. Next you need to modify access rules in your DataServiceConfiguration. You must remove access rule to initial User entity set and add read access rules to that new entity set.

If you need to control access rules per user you have to use some kind of authentication in your service and you must handle this in InitializeService method (unless DataServiceConfiguration is available elsewhere). Something like:

public static void InitializeService(DataServiceConfiguration config)
{
    var context = ServiceSecurityContext.Current;
    if (context != null && context.PrimaryIdentity != null)
    {
        var userName = context.PrimaryIdentity.Name;
        if (SomeMethodToValidateUserPermissions(userName)
        {
            config.SetEntitySetAccessRule("Users", EntitySetRights.AllRead);
        }
    }

    config.SetEntitySetAccessRule("TrimmedUsers", EntitySetRights.AllRead);
} 

By going more deep into WCF there can be other approaches to restrict access to some resources but this one is simplest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文