oData WCF service - hide an element

↘锁芯ラ 提交于 2019-12-18 09:08:10

问题


I'm new to WCF. My web project has an ADO.NET Entity Data Model (aka EF edmx), which has the Entity Container Name JobSystemEntities.

I've created a simple oData WCF data service which uses JobSystemEntities, and it works great:

public class JobService : DataService<JobSystemEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Jobs", EntitySetRights.ReadSingle);
    }

However, this exposes all of the properties on the Job. I would like to hide sensitive data, i.e. the Cost field/property/column of the Job table.


回答1:


I am posting this a but late, but it might help others.

You can use the IgnoreProperties attribute http://msdn.microsoft.com/en-us/library/system.data.services.ignorepropertiesattribute.aspx on your class.

You will have to define a partial Job class in order to do this. Something in the lines of:

namespace DAL.Entities
{
    [IgnoreProperties("Cost")]
    public partial class Job
    {

    }
}



回答2:


I've done something similar to this. A good starting point is found here:

http://weblogs.asp.net/rajbk/archive/2010/05/15/pre-filtering-and-shaping-odata-feeds-using-wcf-data-services-and-the-entity-framework-part-1.aspx

Basically you will need to separate the protected properties of an entity into a separate entity that is linked as a property of the other. Once that is done user a Query Interceptor to restrict when that protected entity can be viewed.

[QueryInterceptor("YourObjectsProtectedProperties")]
public Expression<Func<YourObjectsProtectedProperties, bool>> OnReadYourObjectsProtectedProperties()
{
if (ShowEntityToUser())
   return o => true == true;
return o => true == false;
}


来源:https://stackoverflow.com/questions/4792162/odata-wcf-service-hide-an-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!