public Task <IApiKey> ProvideAsync(string key) { IApiKey retKey = keyRepo.GetApiKey(); return(retKey.Key.Equals(key) ? Task.FromResult(retKey) : Task.FromResult <IApiKey>(null)); }
private async Task <ServiceWrapper> _useClient(string requesterId) { var key = await _keyRepository.GetApiKey("YouTube", requesterId); if (key is null) { throw new NoKeyAvailableException(); } var client = new ServiceWrapper(requesterId, key); await _serviceRequestLogger .LogRequest( key, requesterId, Environment.StackTrace); return(client); }
public override void OnActionExecuting(HttpActionContext actionContext) { string token; try { var queryString = actionContext.Request.RequestUri.ParseQueryString(); if (!string.IsNullOrEmpty(queryString["Authorization-Token"])) { token = queryString["Authorization-Token"]; } else { actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) { Content = new StringContent("Missing Authorization-Token") }; return; } } catch (Exception) { actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) { Content = new StringContent("Missing Authorization-Token") }; return; } try { if (_apiKeyRepository.GetApiKey(token)) { //some code } else { actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden) { Content = new StringContent("Unauthorized User") }; return; } base.OnActionExecuting(actionContext); } catch (Exception) { actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError) { Content = new StringContent("Error encountered while attempting to process authorization token") }; } }
public IHttpActionResult GetAPiKey(string applicationName, string name) { try { return(Ok(keyController.GetApiKey(applicationName, name))); } catch (Exception ex) { return(Error(ex)); } }