To do a feature to prevent an IP attack site, think of using the cache, which has the add and insert methods. There are many parameters in the method, he is stored in key-value way, the difference is that once the Add method is created, the data inside can not be modified. Inset can change the value parameter without changing the key.
The design idea is this, the key is the number of ip,value. The code is as follows:
1 /// <summary>2 ///gets the cache value of the current application specified CacheKey3 /// </summary>4 /// <param name= "CacheKey" ></param>5 /// <returns></returns>6 Public Static ObjectGetCache (stringCacheKey)7 {8System.Web.Caching.Cache Objcache =Httpruntime.cache;9 returnObjcache[cachekey];Ten } One A /// <summary> - ///sets the cache value for the current application to specify CacheKey - /// </summary> the /// <param name= "CacheKey" ></param> - /// <param name= "Objobject" ></param> - /// <param name= "M" >minutes</param> - Public Static voidSetcache (stringCacheKey,ObjectObjobject,intm) + { -System.Web.Caching.Cache Objcache =Httpruntime.cache; +Objcache.insert (CacheKey, Objobject,NULL, DateTime.Now.AddMinutes (M), cache.noslidingexpiration); A } at /// <summary> - /// - /// </summary> - /// <param name= "IP" ></param> - /// <param name= "duration" >often (minutes)</param> - /// <param name= "Count" >maximum number of visits valve value</param> in /// <returns></returns> - Public Static BOOLIpConfig (stringIpintDuration =1,intCount =Ten) to { + BOOLresult =false; - stringUrlindex ="~/css/ipconfig.xml"; the stringFileName =System.Web.HttpContext.Current.Server.MapPath (urlindex); *XDocument doc =xdocument.load (FileName); $ Panax Notoginseng varrel = fromPinchDoc. Descendants ("Item")whereP.attribute ("IP"). Value.tolower () = = IPSelectp; -result = (rel! =NULL&& rel. Count () >0) ?true:false; the if(!result) + { A Objectobj =NULL; the if(string. Isnullorwhitespace (IP)) {result =true; } + Else - { $obj =GetCache (IP); $ if(obj! =NULL) - { - int_count = (int) obj; the if(_count >=count) - {Wuyi if(_count = =count) the { - insertip (IP); Wu sendmobile (IP); - } About return true; $ } - Else - { -System.Web.Caching.Cache Objcache =Httpruntime.cache; AObjcache.insert (IP, _count +1,NULL, DateTime.Now.AddSeconds (duration), cache.noslidingexpiration); + } the } - Else $ { theSetcache (IP,1, duration); the } the } the } - returnresult; in } the the About #regioninserting IP into XML the Public Static voidInsertip (stringIP) the { the stringUrlindex ="~/css/ipconfig.xml"; + stringFileName =System.Web.HttpContext.Current.Server.MapPath (urlindex); -XmlDocument xmldoc =NewXmlDocument (); the xmldoc.load (FileName);BayiXmlNode root = Xmldoc.selectsinglenode ("Root"); theXmlNode root0 = root. Childnodes.item (0); theXmlElement xe1 = xmldoc.createelement ("Item"); -Xe1. SetAttribute ("IP", IP); - the root0. AppendChild (XE1); the Xmldoc.save (FileName); the } the #endregion - the
Cache determines the number of attacks in IP minutes