C # site IP access frequency limits the implementation method for a single site _c# tutorial

Source: Internet
Author: User
Tags datetime set time static class

  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.

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.