site IP Access frequency limit for individual sites
Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Web; <summary>//IP Access Frequency control/</summary> public static class Ipcachemanager {///<summary>///IP Cache
Set///</summary> private static list<ipcacheinfo> dataList = new list<ipcacheinfo> ();
private static Object lockobj = new Object ();
<summary>///for a period of time, the maximum number of requests must be greater than or equal to 1///</summary> private static int maxtimes = 3;
<summary>///for a length of time (in seconds), must be greater than or equal to 1///</summary> private static int partsecond = 30;
<summary>///request denied whether to add the requested number///</summary> private static bool Isfailaddin = FALSE;
Static Ipcachemanager () {}///<summary>///set time, default maxtimes=3, partsecond=30///</summary> <param name= "_maxtimes" > Max request </param>///<param name= "_partsecond" > Request Unit time </param> Publ IC static void settime (int _maxtimes, int _partsecond) {maxtimes = _maxtimes;
Partsecond = _partsecond; ///<summary>///detects whether the number of IP requests can continue to request///and use///</summary>///<param name= "IP" for a period of time >&
lt;/param>///<returns></returns> public static bool Checkisable (string IP) {lock (Lockobj)
{var item = Datalist.find (P => p.ip = = IP);
if (item = = NULL) {item = new Ipcacheinfo (); Item.
ip = IP; Item.
Reqtime.add (DateTime.Now);
Datalist.add (item);
return true; } else {if (item). Reqtime.count > Maxtimes) {item.
Reqtime.removeat (0);
var nowtime = DateTime.Now; if (Isfailaddin) {#region request is rejected, it also needs to be added to the request item.
Reqtime.add (Nowtime); if (item. Reqtime.count >= maxtimes) {if (item). Reqtime[0]. AddSeconds (Partsecond) > Nowtime) {return false;
else {return true;
} else {return true; #endregion} else {#region request is rejected without the need to join the IF (item) request. Reqtime.count >= maxtimes) {if (item). Reqtime[0].
AddSeconds (Partsecond) > Nowtime) {return false; } else {item.
Reqtime.add (Nowtime);
return true; } else {item.
Reqtime.add (Nowtime);
return true;
#endregion}}}} public class Ipcacheinfo {public string IP {get; set;}
Private list<datetime> reqtime = new list<datetime> ();
Public list<datetime> Reqtime {get {return this.reqtime;}
set {this.reqtime = value;} }
}
Above this C # site IP Access frequency restrictions on the implementation of a single site is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.