Cache determines the number of attacks in IP minutes

Source: Internet
Author: User

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

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.