Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm assuming that you trying to consume your service using some Http Protocol based transport (SOAP, REST etc). I'm also assuming that what you want is to authorize the caller using the supplied API key. If both of those conditions apply to your question, you can read on.</p> <p>I recently had to tackle a similar problem only that I did not pass an API key but a username/password hash combination using some HTTP custom headers. I ultimately solved it by implementing a custom authorization policy that once configured in Web.config hooked nicely into the WCF Pipeline.</p> <p>The snippet below should be enough to get you started. You probably would have to replace the x-ms-credentials-XXX headers by a single one representing your API key.</p> <pre><code>internal class RESTAuthorizationPolicy : IAuthorizationPolicy { public RESTAuthorizationPolicy() { Id = Guid.NewGuid().ToString(); Issuer = ClaimSet.System; } public bool Evaluate(EvaluationContext evaluationContext, ref object state) { const String HttpRequestKey = "httpRequest"; const String UsernameHeaderKey = "x-ms-credentials-username"; const String PasswordHeaderKey = "x-ms-credentials-password"; const String IdentitiesKey = "Identities"; const String PrincipalKey = "Principal"; // Check if the properties of the context has the identities list if (evaluationContext.Properties.Count &gt; 0 || evaluationContext.Properties.ContainsKey(IdentitiesKey) || !OperationContext.Current.IncomingMessageProperties.ContainsKey(HttpRequestKey)) return false; // get http request var httpRequest = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestKey]; // extract credentials var username = httpRequest.Headers[UsernameHeaderKey]; var password = httpRequest.Headers[PasswordHeaderKey]; // verify credentials complete if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) return false; // Get or create the identities list if (!evaluationContext.Properties.ContainsKey(IdentitiesKey)) evaluationContext.Properties[IdentitiesKey] = new List&lt;IIdentity&gt;(); var identities = (List&lt;IIdentity&gt;) evaluationContext.Properties[IdentitiesKey]; // lookup user using (var con = ServiceLocator.Current.GetInstance&lt;IDbConnection&gt;()) { using (var userDao = ServiceLocator.Current.GetDao&lt;IUserDao&gt;(con)) { var user = userDao.GetUserByUsernamePassword(username, password); ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload