C # site IP access frequency limit for a single site

Source: Internet
Author: User
Tags set time

Site IP Access frequency limit for a single site

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;//using System.Linq;usingsystem.web;//<summary>//IP Access Frequency control//</summary> Public Static classipcachemanager{/// <summary>    ///IP Cache Collection/// </summary>      Private StaticList<ipcacheinfo> dataList =NewList<ipcacheinfo>(); Private Static ObjectLockobj =New Object(); /// <summary>     ///the maximum number of requests must be greater than or equal to 1 over time/// </summary>     Private Static intMaxtimes =3; /// <summary>     ///length of time (in seconds), must be greater than or equal to 1/// </summary>     Private Static intPartsecond = -; /// <summary>     ///request denied whether to join request number/// </summary>     Private Static BOOLIsfailaddin =false; StaticIpcachemanager () {}/// <summary>     ///set time, default maxtimes=3, partsecond=30/// </summary>     /// <param name= "_maxtimes" >Maximum number of requests</param>     /// <param name= "_partsecond" >Request Unit Time</param>      Public Static voidSetTime (int_maxtimes,int_partsecond) {Maxtimes=_maxtimes; Partsecond=_partsecond; }    /// <summary>     ///detects whether the number of IP requests can continue to be requested over a period of time///and use/// </summary>     /// <param name= "IP" ></param>     /// <returns></returns>      Public Static BOOLCheckisable (stringIP) {        Lock(lockobj) {varitem = Datalist.find (p = = P.ip = =IP); if(Item = =NULL) {Item=NewIpcacheinfo (); Item. IP=IP; Item.                Reqtime.add (DateTime.Now);                Datalist.add (item); return true; }            Else            {                if(item. Reqtime.count >maxtimes) {Item. Reqtime.removeat (0); }                varNowtime =DateTime.Now; if(Isfailaddin) {#regionRequest denied also need to join the requestitem.                    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                {                    #regionIf the request is denied, you don't need to join the request.if(item. 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 classipcacheinfo{ Public stringIP {Get;Set; } PrivateList<datetime> Reqtime =NewList<datetime>();  PublicList<datetime>Reqtime {Get{return  This. Reqtime;} Set{ This. Reqtime =value;} }}

C # site IP access frequency limit for a single site

Related Article

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.