博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
拒绝频繁IP访问--转载
阅读量:7076 次
发布时间:2019-06-28

本文共 4582 字,大约阅读时间需要 15 分钟。

//首先我们要实现 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 配置访问规则

来源:http://www.cnblogs.com/Fooo/archive/2013/01/27/2878820.html

转载于:https://www.cnblogs.com/xiaoyaodijun/p/6687536.html

你可能感兴趣的文章
ADB环境变量的配置
查看>>
C primer plus 练习题 第七章
查看>>
virtualbox 创建com对象失败
查看>>
几种常见模式识别算法整理和总结
查看>>
Sort Colors leetcode java
查看>>
Length of Last Word leetocde java
查看>>
sharepoint项目遇到的WebDAV和HTTP PUT的安全隐患解决办法
查看>>
JAVA Metrics度量工具 - Metrics Core 翻译
查看>>
汇编之FS段寄存器
查看>>
设计模式(十):Decorator装饰者模式 -- 结构型模式
查看>>
Orchard Application Host
查看>>
iOS 消息转发
查看>>
CentOS修改TimeZone
查看>>
DIOCP3-DIOCP1升级到DIOCP3
查看>>
SQL Server 中WITH (NOLOCK)浅析
查看>>
09网易校园招聘笔试题
查看>>
。一个通俗易懂的HMM例子
查看>>
freeswitch 挂断前执行脚本
查看>>
EffectManager
查看>>
周鸿祎谈柔道战略(转)
查看>>