namespaceNotificationApi.Filters { publicclassApiKeyValidationAttribute : FilterAttribute, IAuthorizationFilter { public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation) { try { var expected = ConfigurationManager.AppSettings.Get("apikey"); var actual = actionContext.Request.Headers.GetValues("Api-Key").FirstOrDefault(); if (String.IsNullOrWhiteSpace(actual) || actual != expected) { thrownew Exception(); } } catch { // some logging would be sweet
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Missing or invalid Api-Key"); var source = new TaskCompletionSource<HttpResponseMessage>(); source.SetResult(actionContext.Response); return source.Task; } return continuation(); } } }
///<summary> /// SelectList - view whats in the Queue table. (email_queue.processed = false) /// http://localhost:50829/api/email/selectlist ///</summary> ///<returns></returns> [HttpGet] public HttpResponseMessage SelectList() { ... }
///<summary> /// Insert a new Email into the Queue (dbo.email_queue) ///</summary> ///<param name="obj"></param> ///<returns></returns> [HttpPost] public HttpResponseMessage Insert([FromBody] EmailModel obj) { ... }
///<summary> /// Process whats in the Queue table /// http://localhost:50829/api/email/process ///</summary> ///<param name="obj"></param> ///<returns></returns> // [HttpPut] - not really a PUT as nothing is sent 'TO' 'Process()' to be persisted [HttpGet] public HttpResponseMessage Process() { ... }
Setup: Consumer
1 2 3 4 5 6 7
var url = "http://localhost:50829/api/email/insert";