2012-01-09 89 views
2

我使用WCF与我的ASP.NET MVC应用程序,我的数据服务从我的(EF 4.1).mdf文件中获取数据。但是,有一些费尔德,我想验证显示,例如:如何只允许从WCF数据服务ServiceOperation访问

public static void InitializeService(DataServiceConfiguration config) 
{ 
    config.SetEntitySetAccessRule("Exercies", EntitySetRights.All); 
    config.SetServiceOperationAccessRule("GetAllExercies", ServiceOperationRights.All); 
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; 
} 
[WebGet] 
public IQueryable<Exercise> GetAllExercies(string name, string pass) 
{ 
    if (Membership.ValidateUser(name, pass)) 
     return CurrentDataSource.Exercies; 
    else 
     return CurrentDataSource.Exercies.Where(e => e.Public == true); 
} 

现在,当用户访问httx://localhost/MyService.svc/Exercies,他们虽然他们都没有给出用户名可以搞定一切并通过。
我临时的解决办法是重新命名GetAllExercies只是Exercies但我不知道有没有什么更好的办法?

回答

2

是的,有一个更好的解决方案:查询拦截器。事实上,在实体集和服务操作中使用相同的名称往往会在某些情况下导致问题($元数据对于客户端而言是“令人困惑的”)。它也不是100%安全的(不妨碍通过一些导航属性访问实体,如果你有这个)。

看到这个http://msdn.microsoft.com/en-us/library/dd744842.aspx。这个想法是,你使auth过滤器成为实体集合查询的一部分,WCF DS服务确保它将在访问实体集合的任何地方使用。

+0

由于拦截器不能不接受参数...我只是用它来禁用实体集访问。对我有没有更好的解决方案? – nvcnvn 2012-01-17 09:26:34

+0

没关系.....我现在使用HttpContext来访问查询字符串。 – nvcnvn 2012-01-17 14:25:21