麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

asp.net實現拒絕頻繁的IP訪問的方法

2024-07-10 13:30:14
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了asp.net實現拒絕頻繁的IP訪問的方法,涉及asp.net針對訪問IP的判斷及配置文件的設置技巧,需要的朋友可以參考下
 

本文實例講述了asp.net實現拒絕頻繁的IP訪問的方法。分享給大家供大家參考,具體如下:

首先我們要實現 IHttpModule接口

using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Web.UI;using System.Web.SessionState;using System.Configuration;namespace MyHttp{  public class UrlReWrite : IHttpModule  {    /// <summary>    /// 單個IP最大連接限制數量    /// </summary>    private int rowCount = Convert.ToInt32(ConfigurationSettings.AppSettings["HttpRowCount"]);    /// <summary>    /// 指定區域時間范圍 單位分    /// </summary>    private int httpTime = Convert.ToInt32(ConfigurationSettings.AppSettings["HttpTime"]);    public void Init(HttpApplication application)    {      application.BeginRequest += (new         EventHandler(this.Application_BeginRequest));      application.EndRequest += (new         EventHandler(this.Application_EndRequest));    }    private void Application_BeginRequest(Object source, EventArgs e)    {      HttpApplication Application = (HttpApplication)source;      HttpContext ctx = Application.Context;      //IP地址      string isIp = ctx.Request.UserHostAddress;      if (ctx.Application["time"] == null)      {        ctx.Application["time"] = DateTime.Now;      }      else      {        DateTime isTime = (DateTime)ctx.Application["time"];        int timeTract = Convert.ToInt32(DateTime.Now.Subtract(isTime).Minutes.ToString());        if (timeTract > (httpTime - 1))        {          ctx.Application["time"] = null;          ctx.Application["myip"] = null;        }      }      if (ctx.Application["myip"] != null && ctx.Application["myip"] is CartIp)      {        CartIp cartIp = (CartIp)ctx.Application["myip"];        cartIp.Insert(isIp);        ctx.Application["myip"] = cartIp;        if (cartIp.GetCount(isIp) > rowCount)        {          ctx.Response.Clear();          ctx.Response.Close();        }      }      else      {        CartIp cartIp = new CartIp();        cartIp.Insert(isIp);        HttpContext.Current.Application["myip"] = cartIp;      }    }    private void Application_EndRequest(Object source, EventArgs e)    {    }    public void Dispose()    {    }  }}

ListIp 類

using System;using System.Collections.Generic;using System.Text;namespace MyHttp{  [Serializable]  public class ListIp  {    private string ip;    private int count;    /// <summary>    /// IP地址    /// </summary>    public string IP    {      get { return ip; }      set { ip = value; }    }    /// <summary>    /// 累加數量    /// </summary>    public int Count    {      get { return count; }      set { count = value; }    }  }  [Serializable]  public class CartIp  {    public CartIp()    {      if (_listIp == null)      {        _listIp = new List<ListIp>();      }    }    private List<ListIp> _listIp;    public List<ListIp> _ListIp    {      get { return _listIp; }      set { _listIp = value; }    }    /// <summary>    /// 添加IP    /// </summary>    public void Insert(string ip)    {      int indexof = ItemLastInfo(ip);      if (indexof == -1)      {        //不存在        ListIp item = new ListIp();        item.IP = ip;        _listIp.Add(item);      }      else      {        _listIp[indexof].Count += 1;      }    }    //判斷IP是否存在    public int ItemLastInfo(string ip)    {      int index = 0;      foreach (ListIp item in _ListIp)      {        if (item.IP == ip)        {          return index;//存在        }        index += 1;      }      return -1;//不存在    }    /// <summary>    /// 獲得IP的數量    /// </summary>    /// <param name="ip"></param>    /// <returns></returns>    public int GetCount(string ip)    {      foreach (ListIp item in _ListIp)      {        if (item.IP == ip)        {          return item.Count;//存在        }      }      return -1;//不存在    }  }}

在web.config 配置訪問規則

<appSettings><add key="HttpRowCount" value="100"/><add key="HttpTime" value="10"/></appSettings><system.web>  <httpModules>  <add name="UrlReWrite" type="MyHttp.UrlReWrite"/> </httpModules></system.web>
 


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产羞羞视频在线免费观看 | 国产成人精品一区二区视频免费 | 青青草华人在线 | av在线免费看网址 | 免费在线观看亚洲 | 国产91精品亚洲精品日韩已满 | 91麻豆精品国产91久久久更新资源速度超快 | 热久久91| 草久在线观看视频 | 少妇淫片免费一级毛片 | 久久久久亚洲美女啪啪 | 看片一区二区三区 | 99精品视频在线免费观看 | 九九热这里只有精品8 | 午夜天堂在线视频 | 欧美乱淫| 黄色成年在线观看 | 日本a v免费观看 | 一级电影免费 | 黄色片网站免费在线观看 | 在线中文资源免费 | av免费在线观看免费 | 欧美高清第一页 | av免费av | 国产激情网 | 在线小视频国产 | 91久久99热青草国产 | 国产成人av在线播放 | 草久影视| 欧美日韩一 | 久久久久久久久久久av | 嗯哈~不行好大h双性 | 成人情欲视频在线看免费 | 亚洲国产成人一区二区 | 黄色特级视频 | 久久精品成人影院 | 久久综合给合久久狠狠狠97色69 | 国产精品.com | 免费人成在线播放 | 亚洲综合视频在线播放 | 中文字幕精品在线播放 |