Asp.net: deny frequent IP Access

Source: Internet
Author: User

// First, we need to implement the ihttpmodule Interface

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
{< br> public class urlrewrite: ihttpmodule
{< br> ///


/// maximum number of connections allowed for a single IP address
///
private int rowcount = convert. toint32 (configurationsettings. appsettings ["httprowcount"]);
///
// time range of the specified region
///
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 address
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) CTX. application ["myip"];
Cartip. insert (isip );
CTX. application ["myip"] = cartip;
If (cartip. getcount (isip)> rowcount)
{
CTX. response. Clear ();
CTX. response. Close ();
}
}
Else
{
Cartip = new cartip ();
Cartip. insert (isip );
Httpcontext. Current. application ["myip"] = cartip;
}
}

Private void application_endrequest (Object source, eventargs E)
{

}

Public void dispose ()
{

}

}
}

 

// Listip class

 

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace myhttp
{
[Serializable]
Public class listip
{
Private string IP;
Private int count;
/// <Summary>
/// IP Address
/// </Summary>
Public String IP
{
Get {return IP ;}
Set {IP = value ;}
}
/// <Summary>
/// Accumulative quantity
/// </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>
/// Add an IP address
/// </Summary>
Public void insert (string IP)
{
Int indexof = itemlastinfo (IP );
If (indexof =-1)
{
// Does not exist
Listip item = new listip ();
Item. IP = IP;
_ Listip. Add (item );
}
Else
{
_ Listip [indexof]. Count + = 1;
}
}
// Determine whether an IP exists
Public int itemlastinfo (string IP)
{
Int Index = 0;
Foreach (listip item in _ listip)
{
If (item. IP = IP)
{
Return Index; // Yes
}
Index + = 1;
}
Return-1; // does not exist
}
/// <Summary>
/// Number of IP addresses obtained
/// </Summary>
/// <Param name = "ip"> </param>
/// <Returns> </returns>
Public int getcount (string IP)
{
Foreach (listip item in _ listip)
{
If (item. IP = IP)
{
Return item. Count; // Yes
}
}
Return-1; // does not exist
}
}
}

 

 

// Configure access rules in Web. config

<Deleetask>

<Add key = "httprowcount" value = "100"/>
<Add key = "httptime" value = "10"/>

</Appsettings>

 

<System. Web>

<Httpmodules>
<Add name = "urlrewrite" type = "myhttp. urlrewrite"/>
</Httpmodules>
</System. Web>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.