如何仅允许来自WCF数据服务ServiceOperation的访问
我将 WCF 与 ASP.NET MVC 应用程序一起使用,我的数据服务从我的 (EF 4.1) .mdf 文件获取数据。但我想通过身份验证显示一些字段,例如:
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 但我不确定是否有更好的方法......
I use WCF with my ASP.NET MVC app, my data service get data from my (EF 4.1) .mdf file. But there is some feild that I want to show with authentication, for example:
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);
}
Now when user access httx://localhost/MyService.svc/Exercies, they can get everything although they are not given the username and pass.
My temporary solution is re name GetAllExercies to just Exercies but I not sure is there any better way...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有一个更好的解决方案:查询拦截器。事实上,对实体集和服务操作使用相同的名称往往会在某些情况下导致问题($metadata 对于客户端来说“令人困惑”)。它也不是 100% 安全(如果有的话,不会阻止通过某些导航属性访问实体)。
请参阅此 http://msdn.microsoft.com/en-us/library/dd744842 .aspx。这个想法是,您将身份验证筛选器作为实体集查询的一部分,并且 WCF DS 服务确保它将在访问实体集的任何地方使用它。
Yes, there is a better solution: query interceptors. In fact using the same name for entity set and service operation tends to lead to problems in certain scenarios (the $metadata is "confusing" for the clients). It's also not 100% secure (doesn't prevent accessing the entity through some navigation property if you have that).
See this http://msdn.microsoft.com/en-us/library/dd744842.aspx. The idea is that you make the auth filter part of the entity set query, and WCF DS Service makes sure that it will be used everywhere that entity set is accessed.