当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net实现拒绝频繁的IP访问的方法

asp.net实现拒绝频繁的IP访问的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

怎样增加雌激素,无字拼图5,6合开奖结果2013

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网