endpoint.tv - How to Do API Key Verification with a WCF WebHttp (REST) Service

Sign in to queue

The Discussion

  • User profile image
    cecilphillip

    Is there going to be a REST Starter Kit version for .NET 4??

  • User profile image
    rojacobs

    Still working on the plan.  Most of the server stuff ended up in .NET 4.  The only part that we don't have is the HttpClient library (which is really cool).  It totally works on .NET 4 if you just grab the source and build it.

  • User profile image
    Steve Scott

    How do we pass it in if we're using the WebReference ServiceClient object?

  • User profile image
    Sander van de Velde

    @Steve Scott: Thanks for your fine solution. I have changed it little so it can also be used with (OData) Service references. I also check the header of the request:
    public string GetAPIKey(OperationContext operationContext)
    {
       var request = operationContext.RequestContext.RequestMessage;
       var requestProp =
          (HttpRequestMessageProperty)request.
                Properties[HttpRequestMessageProperty.Name];
       NameValueCollection queryParams =
          HttpUtility.ParseQueryString(requestProp.QueryString);
      string apiKey = queryParams[APIKEY];
      if (apiKey == null)
      {
        apiKey = requestProp.Headers[APIKEY];
      }
      return apiKey;
    }
    and on the client, i pass the ApiKey in the header using the SendingRequest event:
    service.SendingRequest += new 
      EventHandler<System.Data.Services.Client.SendingRequestEventArgs>
        (service_SendingRequest);
     
    static void service_SendingRequest(object sender,
      System.Data.Services.Client.SendingRequestEventArgs e)
    {
       e.Request.Headers.Add("APIkey", "918704ec-4811-45b6-a169-16bae3df69a8");
    }
     See also:
    http://adi.atosoriginblog.nl/2010/12/28/flexibel-apikey-gebruik-bij-wcf-data-services/

Add Your 2 Cents