明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

asp.net : 拒绝频繁的IP访问

Posted on 2013-01-27 15:24  且行且思  阅读(2660)  评论(0编辑  收藏  举报

//首先我们要实现 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>