如何:启用对数据服务的访问(WCF 数据服务)

在 WCF 数据服务 中,您必须显式授予对数据服务公开的资源的访问权限。 这意味着您在创建新的数据服务之后,仍必须显式提供对实体集形式的各个资源的访问。 本主题介绍如何启用对您在完成快速入门后创建的 Northwind 数据服务中的五个实体集的读写访问。 由于 EntitySetRights 枚举是通过使用 FlagsAttribute 定义的,因此您可以使用逻辑 OR 运算符来为单个实体集指定多个权限。

Dd728277.note(zh-cn,VS.100).gif注意:
任何可以访问该 ASP.NET 应用程序的客户端也能够访问由数据服务公开的资源。在生产数据服务中,为防止对资源进行未经授权的访问,还应保护应用程序本身的安全。有关更多信息,请参见Securing ASP.NET Web Sites

启用对数据服务的访问

  • 在数据服务的代码中,用下列代码替换 InitializeService 函数中的占位符代码:

    ' Grant only the rights needed to support the client application.
    config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead _
         Or EntitySetRights.WriteMerge _
         Or EntitySetRights.WriteReplace)
    config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead _
        Or EntitySetRights.AllWrite)
    config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead)
    
     // Grant only the rights needed to support the client application.
    config.SetEntitySetAccessRule("Orders", EntitySetRights.AllRead 
         | EntitySetRights.WriteMerge 
         | EntitySetRights.WriteReplace );
     config.SetEntitySetAccessRule("Order_Details", EntitySetRights.AllRead
         | EntitySetRights.AllWrite);
     config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);
    

    这使得客户端能够对 OrdersOrder_Details 实体集进行读写访问,以及对 Customers 实体集进行只读访问。

另请参见

任务

如何:开发在 IIS 上运行的 WCF 数据服务

概念

配置数据服务(WCF 数据服务)