【问题标题】:WCF Data Service set access rule for table columns instead of the whole tableWCF 数据服务为表列而不是整个表设置访问规则
【发布时间】:2014-03-11 14:45:46
【问题描述】:

在我的 wcf 数据服务中,我通过以下方式阻止客户修改客户:

// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
    // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

    config.SetEntitySetAccessRule("Customers", EntitySetRights.None); // <------- HERE

    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}

有没有办法在表customers 的特定列上设置规则? 例如,我只想在 CustomerPassword 列上启用阅读

解决方案是将客户的所有我不希望客户修改的部分移动到单独的表中。 这种方法需要我对我的数据库进行大量更改如果我可以在表列而不是整个表上设置权限,那就太棒了。

此外,即使我将不希望客户修改的所有客户列(例如 CustomerPassword、DateConnected 等)移动到何处,我将如何防止客户修改该客户的 ID。总会有一列我无法保护。

【问题讨论】:

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


    【解决方案1】:

    这可以通过 ChangeInterceptor 来完成。例如,如果您想允许客户修改客户但不允许他们更改 md5 密码,请执行以下操作:

    [ChangeInterceptor("Customers")] // table to query intercept
    public void WindowsServiceChange(Customer customerEntity, UpdateOperations operations)
    {            
            // make sure following colums are not changed
            if (this.CurrentDataSource.Entry(customerEntity).Property("Password").IsModified)
            {
                // client attempted to update a column he was not supposed to update
                throw new DataServiceException(400, "Access to update column denied");
            }
    
            // else do nothing
    }
    

    将此方法放在数据服务中,每次客户端尝试修改或更新客户时,都会通过该方法。该方法还可以帮助您验证客户的属性。甚至在将其插入数据库之前更新其属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2011-06-28
      • 2011-06-24
      • 2013-08-23
      • 1970-01-01
      相关资源
      最近更新 更多