Ron Jacobs 有篇文章介绍如何在WCF Rest Service中使用API Key验证:http://blogs.msdn.com/b/rjacobs/archive/2010/06/14/how-to-do-api-key-verification-for-rest-services-in-net-4.aspx ,在WCF Data Service中怎么做呢?本文将介绍如何在WCF Data Service中使用API Key进行验证,主要代码来自于Ron Jacobs的这篇文章.
API Key作为一个参数在URL中传递, 在Rob Jacobs的WCFWebHttpLibrary.APIKeyAuthorization的方法string GetAPIKey(OperationContext operationContext)的代码如下:
string GetAPIKey(OperationContext operationContext)
2: {
// Get the request message
4: var request = operationContext.RequestContext.RequestMessage;
// Get the HTTP Request
6: var requestProp =(HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
// Get the query string
8: NameValueCollection queryParams =
9: HttpUtility.ParseQueryString(requestProp.QueryString);
10:
// Return the API key (if present, null if not)
string apiKey = queryParams[APIKEY];
// Is the API Key available in the querystring?
null)
15: {
// Is the API Key available in the header?
17: apiKey = requestProp.Headers[APIKEY];
18: }
return apiKey;
20: }