加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 站点IP访问频率限制 针对单个站点的实现方法

发布时间:2020-12-15 06:15:23 所属栏目:百科 来源:网络整理
导读:站点IP访问频率限制 针对单个站点 using System;using System.Collections.Generic;using System.IO;//using System.Linq;using System.Web;// summary// IP访问频率控制// /summary public static class IPCacheManager{ /// summary /// IP缓存集合 /// /s

 站点IP访问频率限制 针对单个站点

using System;
using System.Collections.Generic;
using System.IO;
//using System.Linq;
using System.Web;

// <summary>
// IP访问频率控制
// </summary> 
public static class IPCacheManager
{

  /// <summary>
  /// IP缓存集合 
  /// </summary> 
  private static List<IPCacheInfo> dataList = new List<IPCacheInfo>();
  private static object lockObj = new object();

  /// <summary> 
  /// 一段时间内,最大请求次数,必须大于等于1 
  /// </summary> 
  private static int maxTimes = 3;

  /// <summary> 
  /// 一段时间长度(单位秒),必须大于等于1 
  /// </summary> 
  private static int partSecond = 30;

  /// <summary> 
  /// 请求被拒绝是否加入请求次数 
  /// </summary> 
  private static bool isFailAddIn = false;

  static IPCacheManager()
  {
  }

  /// <summary> 
  /// 设置时间,默认maxTimes=3,partSecond=30 
  /// </summary> 
  /// <param name="_maxTimes">最大请求次数</param> 
  /// <param name="_partSecond">请求单位时间</param> 
  public static void SetTime(int _maxTimes,int _partSecond)
  {
    maxTimes = _maxTimes;
    partSecond = _partSecond;
  }

  /// <summary> 
  /// 检测一段时间内,IP的请求次数是否可以继续请求 
  /// 和使用 
  /// </summary> 
  /// <param name="ip"></param> 
  /// <returns></returns> 
  public static bool CheckIsAble(string ip)
  {
    lock (lockObj)
    {
      var item = dataList.Find(p => p.IP == ip);
      if (item == null)
      {
        item = new IPCacheInfo();
        item.IP = ip;
        item.ReqTime.Add(DateTime.Now);
        dataList.Add(item);

        return true;
      }
      else
      {
        if (item.ReqTime.Count > maxTimes)
        {
          item.ReqTime.RemoveAt(0);
        }

        var nowTime = DateTime.Now;
        if (isFailAddIn)
        {
          #region 请求被拒绝也需要加入当次请求
          item.ReqTime.Add(nowTime);
          if (item.ReqTime.Count >= maxTimes)
          {
            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)
            {
              return false;
            }
            else
            {
              return true;
            }
          }
          else
          {
            return true;
          }
          #endregion
        }
        else
        {
          #region 请求被拒绝就不需要加入当次请求了
          if (item.ReqTime.Count >= maxTimes)
          {
            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)
            {
              return false;
            }
            else
            {
              item.ReqTime.Add(nowTime);
              return true;
            }
          }
          else
          {
            item.ReqTime.Add(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 this.reqTime; }
    set { this.reqTime = value; }
  }
}

以上这篇C# 站点IP访问频率限制 针对单个站点的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读