Как бы вы реализовали API-ключ в службе данных WCF?
Есть ли способ требовать ключ API в URL-адресе / или какой-либо другой способ передачи службе секретного ключа для предоставления доступа к данным?
У меня есть это прямо сейчас ...
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);
}
}
... Это работает, но не идеально, потому что вы не можете получить метаданные и обнаружить службу через проводник Add Service Reference. Я мог бы проверить, есть ли $ метаданные в URL, но это похоже на хак. Есть ли способ лучше?