HOW TO:啟用資料服務的存取 (WCF Data Services)
在 WCF Data Services 中,您必須明確授與資料服務所公開之資源的存取權。 這表示當您建立新的資料服務之後,您仍然必須明確提供個別資源的存取權當做實體集。 本主題示範如何在完成快速入門時所建立的 Northwind 資料服務中,針對其中 5 個實體集啟用讀取和寫入存取權。由於 EntitySetRights 列舉是使用 FlagsAttribute 所定義,因此您可以使用邏輯 OR 運算子,為單一實體集指定多個權限。
注意
任何可以存取 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);
如此可讓用戶端具有 Orders 和 Order_Details 實體集的讀取和寫入存取權,並擁有 Customers 實體集的唯讀存取權。
請參閱
工作
HOW TO:開發在 IIS 上執行的 WCF Data Service