private bool IsAllowed(string controllerName, string actionName, string ipAddress)
{
var isAllowed = true;
var maxRequests = 3;
var timeframeMins = 3;
var cacheKey = $"{controllerName}-{actionName}-{ipAddress}";
MemoryCache cache = MemoryCache.Default;
CacheItemPolicy policy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(timeframeMins),
Priority = System.Runtime.Caching.CacheItemPriority.Default,
};
if (cache.Get(cacheKey) == null)
{
cache.Add(cacheKey, 1, policy);
}
else
{
var hitCount = (int)cache.Get(cacheKey);
hitCount++;
cache[cacheKey] = hitCount;
if (hitCount > maxRequests)
{
isAllowed = false;
}
}
return isAllowed;
}
private bool IsAllowed(string controllerName, string actionName, string ipAddress)
{
var isAllowed = true;
var maxRequests = 3;
var timeframeMins = 3;
var cacheKey = $"{controllerName}-{actionName}-{ipAddress}";
MemoryCache cache = MemoryCache.Default;
CacheItemPolicy policy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.UtcNow.AddMinutes(timeframeMins),
Priority = System.Runtime.Caching.CacheItemPriority.Default,
};
if (cache.Get(cacheKey) == null)
{
cache.Add(cacheKey, 1, policy);
}
else
{
var hitCount = (int)cache.Get(cacheKey);
hitCount++;
cache[cacheKey] = hitCount;
if (hitCount > maxRequests)
{
isAllowed = false;
}
}
return isAllowed;
}