//首先我们要实现 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 - ))
{
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 == -)
{
//不存在
ListIp item = new ListIp();
item.IP = ip;
_listIp.Add(item);
}
else
{
_listIp[indexof].Count += ;
}
}
//判断IP是否存在
public int ItemLastInfo(string ip)
{
int index = ;
foreach (ListIp item in _ListIp)
{
if (item.IP == ip)
{
return index;//存在
}
index += ;
}
return -;//不存在
}
/// <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 -;//不存在
}
}
} //在web.config 配置访问规则 <appSettings> <add key="HttpRowCount" value=""/>
<add key="HttpTime" value=""/> </appSettings> <system.web> <httpModules>
<add name="UrlReWrite" type="MyHttp.UrlReWrite"/>
</httpModules>
</system.web>

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

拒绝频繁IP访问--转载的更多相关文章

  1. Nginx 拒绝指定IP访问

    来源 : http://www.ttlsa.com/nginx/nginx-deny-ip-access/   闲来无事,登陆服务器,发现有个IP不断的猜测路径.试图往服务器上传文件(木马).于是查看 ...

  2. nginx拒绝国外IP访问

    nginx拒绝国外IP访问方法很多,比如iptables,geoip模块,域名解析等等.这些方法不会相互冲突,可以结合起来一起使用. 今天来教大家利用两个小方法解决  域名解析禁止掉海外IP访问网站. ...

  3. nginx 拒绝本地ip访问

    server { listen default_server; server_name _; server_name localhost; ; }

  4. linux配置IP访问权限

    允许访问vi /etc/hosts.allow添加(可以添加多行,其中“:allow”可以省率)sshd:192.168.81.*:allow                     #表示192.1 ...

  5. 【转载】阿里云ECS Linux服务器禁止某些IP访问

    在阿里云ECS Linux服务器运维过程中,如果发现某些IP访问异常,如怀疑有攻击行为或者怀疑是别人写的爬虫程序长时间占用你的服务器资源,则可以通过相关的设置来禁止这些IP段的访问,拒绝这些IP的请求 ...

  6. 【转载】 腾讯云通过设置安全组禁止某些IP访问你的服务器

    有时候我们在运维网站的过程中会发现一些漏洞扫描者的IP信息,或者个人爬虫网站的IP信息,此时我们想禁止掉这些IP访问到你的服务器,可以通过腾讯云的安全组功能来设置禁止这些IP访问你的服务器,也可以通过 ...

  7. 【转载】 禁止国外IP访问你的网站

    在网站的运维过程中,我们通过网站记录的IP列表记录有时候会发现很多国外的IP的访问,如美国的IP等,而很多的服务器攻击行为的发起点很有可能在国外,此时为了服务器安全的考虑,我们可以考虑禁止国外IP访问 ...

  8. 【转载】使用宝塔Linux面板屏蔽某些IP访问你的服务器

    在服务器的运维过程中,有时候发现一些异常IP或者扫描漏洞攻击者IP访问你的网站,此时如果想屏蔽该IP访问你的服务器,可以通过云服务器厂商提供的安全组进行设置.如果服务器安装有宝塔面板,也可以通过宝塔面 ...

  9. linux安全篇:禁止频繁访问的ip访问nginx

    实验环境 版本:redhat6.5ip:172.16.1.100,172.16.10软件:nginx 172.16.1.10部署nginx [root@localhost tools]# lsngin ...

随机推荐

  1. C++写文件

    头文件 ofstream -- 向文件写内容 实现代码 #include <vector> #include <string> #include <fstream> ...

  2. Linux内存分配小结--malloc、brk、mmap【转】

    转自:https://blog.csdn.net/gfgdsg/article/details/42709943 http://blog.163.com/xychenbaihu@yeah/blog/s ...

  3. Vue.js 子组件的异步加载及其生命周期控制

    前端开发社区的繁荣,造就了很多优秀的基于 MVVM 设计模式的框架,而组件化开发思想也越来越深入人心.这其中不得不提到 Vue.js 这个专注于 VM 层的框架. 本文主要对 Vue.js 组件化开发 ...

  4. linux虚拟机网络服务问题

    这里说一下我遇到的一个网络问题,前天修改了虚拟机的主机名,重启虚拟机之后,使用新的主机名和IP都可以访问虚拟机,但昨天开启虚拟机之后,宿主机使用主机名和IP都不能访问虚拟机,于是,我通过ifconfi ...

  5. P3830 [SHOI2012]随机树 题解

    P3830 随机树 坑题,别人的题解我看了一个下午没一个看得懂的,我还是太弱了. 题目链接 P3830 [SHOI2012]随机树 题目描述 输入输出格式 输入格式: 输入仅有一行,包含两个正整数 q ...

  6. C++面向对象的特点

    C++面向对象的特点 面向对象的特点主要有: 封装, 继承, 多态; 现在自己的简单理解如下, 但要明白具体怎么实现, 背后的原理是什么? 什么是封装, C++怎么实现封装 封装的大致可以分为: 函数 ...

  7. MybatisGenerator生成的mapper 少了识别主键的方法 byPrimaryKey()

    生成的文件缺少红线标注的类似方法 添加 <property name="useInformationSchema" value="true"/>即可 ...

  8. MicroPython的开发板

    比如: pyboard micro:bit ESP8266/ESP32 stm32等等 什么是pyboard? pyboard是官方的MicroPython微控制器板,完全支持软件功能.硬件有: ST ...

  9. 洛谷P5072 [Ynoi2015]盼君勿忘 [莫队]

    传送门 辣鸡卡常题目浪费我一下午-- 思路 显然是一道莫队. 假设区间长度为\(len\),\(x\)的出现次数为\(k\),那么\(x\)的贡献就是\(x(2^{len-k}(2^k-1))\),即 ...

  10. PKUWC2019垫底记

    凭着noip2018中超凡的运气,我来到了纪中. DAY0 听说PKUWC可以看榜?那就不用担心写挂啦!开心! 刚从雅礼回来休息了一天,下午就和hz一起坐上教练的车去到了中山纪中. 纪中好大好漂亮啊! ...