The example in this article describes the method by which ASP.net implementations deny frequent IP access. Share to everyone for your reference, specific as follows:
First we have 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 {public class Urlrewrite:ihttpmodule {///<summary>///single IP maximum connection limit///
ummary> private int rowcount = Convert.ToInt32 (configurationsettings.appsettings["Httprowcount")); <summary>///Specify area time range unit///</summary> private int httptime = Convert.ToInt32 (Configuration
settings.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 = (Httpapp
lication) 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 = (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 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>///Cumulative quantity///</summary> public int Count {get {return count;}
set {count = value;} } [Serializable] public class Cartip {public Cartip () {if (_listip = = null) {_lis
TIp = new list<listip> ();
} private list<listip> _listip;
Public list<listip> _listip {get {return _listip;}
set {_listip = value;} ///<summary>///Add IP///</summary> public void Insert (string ip) {int indexof
= Itemlastinfo (IP); if (indexof = = 1) {//no Listip item =New Listip (); Item.
ip = IP;
_listip.add (item); } else {_listip[indexof].
Count + 1;
}//Determine if IP exists public int itemlastinfo (string IP) {int index = 0; foreach (Listip item in _listip) {if (item).
ip = = IP) {return index;//exists} index = 1; return-1;//does not exist}///<summary>///get the number of 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;//exists}} return-1;//does not exist}}
Configuring access rules in Web.config
<appSettings>
<add key= "Httprowcount" value= "/> <add key=" httptime "value="/>
</appSettings>
<system.web>
More interested in asp.net related content readers can view the site topics: "asp.net file Operation skills Summary", "ASP.net ajax Skills Summary" and "asp.net cache operation skills Summary."
I hope this article will help you to ASP.net program design.