public override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity, RateLimitCounter counter, RateLimitRule rule) { base.LogBlockedRequest(httpContext, identity, counter, rule); var key = $"blockedIp_{identity.ClientIp}"; _memoryCache.TryGetValue(key, out int blockedCount); blockedCount++; if (blockedCount > 10) { _blockIpService.BlockIpAsync(identity.ClientIp, false); _logger.LogInformation($"Blocked {identity.ClientIp}"); } else { _memoryCache.Set(key, blockedCount, new MemoryCacheEntryOptions().SetSlidingExpiration(new System.TimeSpan(0, 5, 0))); } }
public override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity, RateLimitCounter counter, RateLimitRule rule) { base.LogBlockedRequest(httpContext, identity, counter, rule); var key = $"blockedIp_{identity.ClientIp}"; _memoryCache.TryGetValue(key, out int blockedCount); blockedCount++; if (blockedCount > 10) { _blockIpService.BlockIpAsync(identity.ClientIp, false); _logger.LogInformation(Constants.BypassFiltersEventId, null, "Banned {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext)); } else { _logger.LogInformation(Constants.BypassFiltersEventId, null, "Request blocked {0}. \nInfo: \n{1}", identity.ClientIp, GetRequestInfo(httpContext)); _memoryCache.Set(key, blockedCount, new MemoryCacheEntryOptions().SetSlidingExpiration(new TimeSpan(0, 5, 0))); } }