SoFunction
Updated on 2025-03-06

C# Site IP access frequency limit Implementation method for a single site

 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;
  }

  /// &lt;summary&gt; 
  /// Detect whether the number of IP requests can continue to request over a period of time  /// and use  /// &lt;/summary&gt; 
  /// &lt;param name="ip"&gt;&lt;/param&gt; 
  /// &lt;returns&gt;&lt;/returns&gt; 
  public static bool CheckIsAble(string ip)
  {
    lock (lockObj)
    {
      var item = (p =&gt;  == ip);
      if (item == null)
      {
        item = new IPCacheInfo();
         = ip;
        ();
        (item);

        return true;
      }
      else
      {
        if ( &gt; maxTimes)
        {
          (0);
        }

        var nowTime = ;
        if (isFailAddIn)
        {
          #region Requests are denied and need to be added to the request.          (nowTime);
          if ( &gt;= maxTimes)
          {
            if ([0].AddSeconds(partSecond) &gt; 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 ( &gt;= maxTimes)
          {
            if ([0].AddSeconds(partSecond) &gt; nowTime)
            {
              return false;
            }
            else
            {
              (nowTime);
              return true;
            }
          }
          else
          {
            (nowTime);
            return true;
          }
          #endregion
        }
      }
    }
  }
}

public class IPCacheInfo
{
  public string IP { get; set; }

  private List&lt;DateTime&gt; reqTime = new List&lt;DateTime&gt;();
  public List&lt;DateTime&gt; 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.