//首先我们要实现 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. Java基础3-数组操作;类概述

    昨日内容回顾 数据类型 基本数据类型 1) byte, short, int, long, float, double 2) boolean[true, false] 3) char 100: 默认为 ...

  2. [debug]记一次竞态更新bug的解决

    公司的django项目,有一个旧接口,使用POST方法更新用户的一种记录型数据. 这个接口的历史有点长,最早的时候没有那么多需求,只会更新两个布尔字段.后来,加入一个需要高频次记录的字段.这些字段都属 ...

  3. 开源录屏工具 Best Screen Recording Open Source Software For Windows 2017

    OBS Studio OBS (Open Broadcaster Software) - Free and open source software for live streaming and sc ...

  4. node ,npm和nvm 版本的管理

    node npm :node 的包管理 nvm :node 的版本管理 node -v ---->查看node 的版本  (v---->version) npm -v ----->n ...

  5. Centos 6 安装FreeSWITCH

    为了安装FreeSWITCH ,我选择的Linux是CentOS,目前最新的Centos版本是6.具体安装CentOS的是步骤详见网上的其它资料,本节的主要目的是为了记录FreeSWITCH的安装过程 ...

  6. CANopen--Copley驱动器 ACJ-055-18 过程数据PDO的断电保存方法

    Copley CANopen 系列驱动器中,均保存有默认的 PDO 设置, 如下图所示 实际使用中,往往需要修改默认的 PDO 配置,以映射不同模式或功能下的 PDO 对象,但由于 PDO 配置无法存 ...

  7. scp命令:远程复制粘贴文件

    文章链接:https://www.cnblogs.com/webnote/p/5877920.html scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有 ...

  8. python 安装mysql报错

    原 安装Python mysqlclient出现“OSError: mysql_config not found”错误 2016年06月01日 12:15:11 wangtaoking1 阅读数:11 ...

  9. Confluence 6 布局高级自定义

    重载 Velocity 模板 velocity 目录是 Confluence Velocity 模板文件进行搜索时候需要的文件夹.例如,你可以通过将你的 Velocity 文件使用正确的文件名放置到正 ...

  10. npx简介(转载)

    npm v5.2.0引入的一条命令(npx),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验. 举例:使用create-react-app创建一个react项目. 老方法: npm ...