public async Task InvokeAsync(HttpContext context) { // add user log var request = context.Request; var authToken = request.Headers["Authorization"].ToString(); if (!authToken.IsNullOrEmptyOrSpace() && _cache.Enable()) { var token = authToken.GetToken(); var count = _cache.GetStringAsync(authToken); if (!count.Result.IsNullOrEmptyOrSpace()) { var curCount = count.Result.ToInt() + 1; if (curCount > _config.GetAppSetting("ThrottlePerUser").ToInt()) { _logger.LogError($"Throttle: {authToken}"); context.Response.StatusCode = 401; await context.Response.WriteAsync("Token has used more than " + $"{_config.GetAppSetting("ThrottlePerUser")} times. Please login again."); return; } _cache.SetStringAsync(authToken, curCount.ToStringEx(), new DistributedCacheEntryOptions() { AbsoluteExpiration = token.ValidTo }); } else { _cache.SetStringAsync(authToken, "1", new DistributedCacheEntryOptions() { AbsoluteExpiration = token.ValidTo }); } } await _next(context); }
public ActionResult Get([FromHeader(Name = "Authorization")] string authentication, string owner, string plant, string valvetype, string filetype, string tagnumber = "", string serialnumber = "") { // validate filter parameters var message = string.Empty; var token = authentication.GetToken(); var filters = new List <string>() { owner, plant, valvetype, filetype, tagnumber, serialnumber }; var valves = _valveService.ValidateValve(token.GetTokenName(TokenConstants.TokenName), filters, ref message); if (valves == null) { _logger.LogInformation($"Get Repair History Failed: {message}-{string.Join(',', filters)}"); return(NotFound(message)); } var file = _cache.GetStringAsync(_cache.GetCacheKey(valves[0])); if (file.Result != null) { return(Ok(file.Result)); } // generate valve files valves = _valveService.GetValves(valves); string fileName; switch (message) { case AppConstants.Xml: fileName = _valveService.CreateValvesFile(valves); break; case AppConstants.Docx: fileName = _reportService.CreateWordFiles(valves, _localizer); break; default: fileName = _valveService.CreateValvesFile(valves); break; } if (fileName == null) { return new ObjectResult("Create Valve Files Failed!") { StatusCode = 500 } } ; _cache.SetStringAsync(_cache.GetCacheKey(valves[0]), fileName, new DistributedCacheEntryOptions() { AbsoluteExpiration = token.ValidTo }); return(Ok(fileName)); } }