网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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
  {
    /// 
    /// 单个IP较大连接限制数量
    /// 
    private int rowCount = Convert.ToInt32(ConfigurationSettings.AppSettings["HttpRowCount"]);
    /// 
    /// 指定区域时间范围 单位分
    /// 
    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;
    /// 
    /// IP地址
    /// 
    public string IP
    {
      get { return ip; }
      set { ip = value; }
    }
    /// 
    /// 累加数量
    /// 
    public int Count
    {
      get { return count; }
      set { count = value; }
    }
  }
  [Serializable]
  public class CartIp
  {
    public CartIp()
    {
      if (_listIp == null)
      {
        _listIp = new List();
      }
    }
    private List _listIp;
    public List _ListIp
    {
      get { return _listIp; }
      set { _listIp = value; }
    }
    /// 
    /// 添加IP
    /// 
    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;//不存在
    }
    /// 
    /// 获得IP的数量
    /// 
    /// 
    /// 
    public int GetCount(string ip)
    {
      foreach (ListIp item in _ListIp)
      {
        if (item.IP == ip)
        {
          return item.Count;//存在
        }
      }
      return -1;//不存在
    }
  }
}

在web.config 配置访问规则






  
  
 

看完上述内容,你们掌握asp.net中怎么禁止频繁的IP访问的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站名称:asp.net中怎么禁止频繁的IP访问-创新互联
新闻来源:http://bjjierui.cn/article/ddijip.html

其他资讯