有沒有一種方法可以要求URL中的API密鑰/或其他方式將服務傳遞給私鑰以授予對數據的訪問權限?如何在WCF數據服務中實現API密鑰?
我現在有這個權利......
using System;
using System.Data.Services;
using System.Data.Services.Common;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Web;
using Numina.Framework;
using System.Web;
using System.Configuration;
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class odata : DataService {
public static void InitializeService(DataServiceConfiguration config) {
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
//config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
protected override void OnStartProcessingRequest(ProcessRequestArgs args) {
HttpRequest Request = HttpContext.Current.Request;
if(Request["apikey"] != ConfigurationManager.AppSettings["ApiKey"])
throw new DataServiceException("ApiKey needed");
base.OnStartProcessingRequest(args);
}
}
...這工作,但因爲你不能在元數據獲取,發現通過添加服務引用探險服務它並不完美。我可以檢查$ metadata是否在url中,但它看起來像一個黑客。有沒有更好的辦法?
這個工作與緩存機械描述在這裏:http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIncludingXMLAndJSONIn30Minutes.aspx – friism 2010-09-05 10:56:33