public async Task <IActionResult> Aka(string akaName) { if (string.IsNullOrWhiteSpace(akaName)) { return(BadRequest()); } var ip = HttpContext.Connection.RemoteIpAddress?.ToString() ?? "N/A"; if (string.IsNullOrWhiteSpace(UserAgent)) { return(BadRequest()); } var token = await _linkForwarderService.GetTokenByAkaNameAsync(akaName); // can not redirect to default url because it will confuse user that the aka points to that default url. if (token is null) { return(NotFound()); } // Do not use RedirectToAction() because another 302 will happen. return(await PerformTokenRedirection(token, ip)); }
public async Task <IActionResult> Aka(string akaName) { bool ValidateAkaName(string name) { return(!string.IsNullOrWhiteSpace(name) && Regex.IsMatch(name, @"^(?!-)([a-z0-9-]+)$")); } try { if (!ValidateAkaName(akaName)) { return(BadRequest()); } var ip = HttpContext.Connection.RemoteIpAddress.ToString(); var ua = Request.Headers["User-Agent"]; if (string.IsNullOrWhiteSpace(ua)) { _logger.LogWarning($"'{ip}' requested akaName '{akaName}' without User Agent. Request is blocked."); return(BadRequest()); } var tokenResponse = await _linkForwarderService.GetTokenByAkaNameAsync(akaName); if (tokenResponse.IsSuccess) { if (tokenResponse.Item == null) { // can not redirect to default url because it will confuse user that the aka points to that default url. return(NotFound()); } // Do not use RedirectToAction() because another 302 will happen. return(await PerformTokenRedirection(tokenResponse.Item, ip, ua)); } return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } catch (Exception e) { _logger.LogError(e, e.Message); return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } }