Site IP access frequency limits For individual sites
using System; using ; using ; //using ; using ; // <summary> // IP access frequency control// </summary> public static class IPCacheManager { /// <summary> /// IP cache collection /// </summary> private static List<IPCacheInfo> dataList = new List<IPCacheInfo>(); private static object lockObj = new object(); /// <summary> /// For a period of time, the maximum number of requests must be greater than or equal to 1 /// </summary> private static int maxTimes = 3; /// <summary> /// The length of a period of time (units of seconds) must be greater than or equal to 1 /// </summary> private static int partSecond = 30; /// <summary> /// Number of times the request was rejected /// </summary> private static bool isFailAddIn = false; static IPCacheManager() { } /// <summary> /// Set time, default maxTimes=3, partSecond=30 /// </summary> /// <param name="_maxTimes">Maximum number of requests</param> /// <param name="_partSecond">Request unit time</param> public static void SetTime(int _maxTimes, int _partSecond) { maxTimes = _maxTimes; partSecond = _partSecond; } /// <summary> /// Detect whether the number of IP requests can continue to request over a period of time /// and use /// </summary> /// <param name="ip"></param> /// <returns></returns> public static bool CheckIsAble(string ip) { lock (lockObj) { var item = (p => == ip); if (item == null) { item = new IPCacheInfo(); = ip; (); (item); return true; } else { if ( > maxTimes) { (0); } var nowTime = ; if (isFailAddIn) { #region Requests are denied and need to be added to the request. (nowTime); if ( >= maxTimes) { if ([0].AddSeconds(partSecond) > nowTime) { return false; } else { return true; } } else { return true; } #endregion } else { #region If the request is denied, there is no need to join the request. if ( >= maxTimes) { if ([0].AddSeconds(partSecond) > nowTime) { return false; } else { (nowTime); return true; } } else { (nowTime); return true; } #endregion } } } } } public class IPCacheInfo { public string IP { get; set; } private List<DateTime> reqTime = new List<DateTime>(); public List<DateTime> ReqTime { get { return ; } set { = value; } } }
The above article C# site IP access frequency limit The implementation method for a single site is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.