//首先我们要实现 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. 2017-2018-2 20165231 实验四 Android程序设计

    实验报告封面 课程:Java程序设计 班级:1652班 姓名:王杨鸿永 学号:20165231 指导教师:娄嘉鹏 实验日期:2018年5月14日 实验时间:15:25 - 17:15 实验序号:实验四 ...

  2. 【VMware vSphere】vCenter添加主机失败:无法访问指定主机

    背景 前一段时间,给一台服务器安装ESXi系统,安装成功之后,通过vCenter在上面安装了一台VDP系统.结果前几天发现服务器掉线,重新连接时出现问题.问题描述如下: 其中错误堆栈具体内容为:在 v ...

  3. MFC修改对话框标题

    对话框标题栏内容为静态 直接在对话框属性"常规"的"Caption"中修改. 动态生成对话框标题栏内容 SetWindowText()函数就可以 CString ...

  4. Vivado Turtorial 01 —— 使用vivado中debug功能(类似ISE中ChipScope)

    1.基于BASYS3板子,有如下代码: module top( input clk, input rst, output test_clk ); parameter DIV_CNT = 2; reg ...

  5. 用vue+element-ui开发后台笔记

    1.前端通过 formData: new FormData(), 构造对象传数值给后台! 当传给后台的参数中有图片的时候,需要把需要传输的数据通过构造对象new FormData()的形式存数据,并且 ...

  6. 部署openfaas

    首先必须部署Docker CE 17.05或者以上版本. 首先卸载旧版本, $ sudo yum remove docker \ docker-common \ docker-selinux \ do ...

  7. (并发编程)RLock(与死锁现象),Semaphore,Even事件,线程Queue

    一.死锁现象与递归锁所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在 ...

  8. Linux root密码忘记了怎么办?

    一.找回root密码: 1. 开机出现 Booting Red Hat Enterprise Linux Client (…) in 3 seconds … 按任意键进入menu菜单: 2. 选择当前 ...

  9. HNUOJ 13341

    题目给你一个串, 串是严格的 1 – n 的排列,里面的数是随机的 把这个串里面的数字分别输出//先预处理,对于给出的串能找到里面的最大数,再 DFS 处理 #include<iostream& ...

  10. 【原创】大数据基础之Flume(2)Sink代码解析

    flume sink核心类结构 1 核心接口Sink org.apache.flume.Sink /** * <p>Requests the sink to attempt to cons ...