title: How would you implement API key in WCF Data Service?
body: <p>Is there a way to require an API key in the URL / or some other way of passing the service a private key in order to grant access to the data?</p>
  
  <p>I have this right now...</p>
  
  <pre><code>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);
      }
  } 
  </code></pre>
  
  <p>...This works but it's not perfect because you cannot get at the metadata and discover the service through the Add Service Reference explorer. I could check if $metadata is in the url but it seems like a hack. Is there a better way?</p>