asp.net implement methods to deny frequent IP access _ practical Tips

Source: Internet
Author: User
Tags httpcontext rowcount

The example in this article describes the method by which ASP.net implementations deny frequent IP access. Share to everyone for your reference, specific as follows:

First we have to implement the IHttpModule interface

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.SessionState;
Using System.Configuration; Namespace Myhttp {public class Urlrewrite:ihttpmodule {///<summary>///single IP maximum connection limit///
    ummary> private int rowcount = Convert.ToInt32 (configurationsettings.appsettings["Httprowcount")); <summary>///Specify area time range unit///</summary> private int httptime = Convert.ToInt32 (Configuration
    settings.appsettings["Httptime"]); public void Init (HttpApplication application) {application. BeginRequest = (new EventHandler (this.
      Application_BeginRequest)); Application. endrequest = (new EventHandler (this.
    Application_EndRequest)); } private void Application_BeginRequest (Object source, EventArgs e) {HttpApplication application = (Httpapp
      lication) source;
      HttpContext ctx = Application.context;
    IP Address  String isIp = CTx.
      request.userhostaddress; if (CTX. application["time"] = = null) {CTX.
      application["Time"] = DateTime.Now; else {datetime istime = (datetime) ctx.
        application["Time"]; int timetract = Convert.ToInt32 (DateTime.Now.Subtract (istime).
        Minutes.tostring ()); if (Timetract > (httpTime-1)) {ctx.
          application["Time" = null; CTx.
        application["Myip"] = null; } if (CTX. application["Myip"]!= null && CTX. application["Myip" is Cartip) {Cartip Cartip = (cartip) ctx.
        application["Myip"];
        Cartip.insert (ISIP); CTx.
        application["Myip"] = Cartip; if (Cartip.getcount (isIp) > RowCount) {ctx.
          Response.Clear (); CTx.
        Response.close ();
        } else {Cartip cartip = new Cartip ();
        Cartip.insert (ISIP);
      httpcontext.current.application["Myip"] = Cartip; }
    }
    private void Application_EndRequest (Object source, EventArgs e) {} public void Dispose () {}}

 }

Listip class

Using System;
Using System.Collections.Generic;
Using System.Text;
    namespace Myhttp {[Serializable] public class Listip {private string IP;
    private int count;
      <summary>///IP Address///</summary> public string IP {get {return IP;}
    set {IP = value;}
      ///<summary>///Cumulative quantity///</summary> public int Count {get {return count;}
    set {count = value;} } [Serializable] public class Cartip {public Cartip () {if (_listip = = null) {_lis
      TIp = new list<listip> ();
    } private list<listip> _listip;
      Public list<listip> _listip {get {return _listip;}
    set {_listip = value;} ///<summary>///Add IP///</summary> public void Insert (string ip) {int indexof
      = Itemlastinfo (IP); if (indexof = = 1) {//no Listip item =New Listip (); Item.
        ip = IP;
      _listip.add (item); } else {_listip[indexof].
      Count + 1;
      }//Determine if IP exists public int itemlastinfo (string IP) {int index = 0; foreach (Listip item in _listip) {if (item).
      ip = = IP) {return index;//exists} index = 1; return-1;//does not exist}///<summary>///get the number of IP///</summary>///<param name= "IP" ></param>///<returns></returns> public int getcount (string ip) {foreach (Listip Item in _listip) {if (item. ip = = IP) {return item.

 count;//exists}} return-1;//does not exist}}

Configuring access rules in Web.config

<appSettings>
<add key= "Httprowcount" value= "/> <add key=" httptime "value="/>
</appSettings>
<system.web>
   
 

More interested in asp.net related content readers can view the site topics: "asp.net file Operation skills Summary", "ASP.net ajax Skills Summary" and "asp.net cache operation skills Summary."

I hope this article will help you to ASP.net program design.

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.