C # Implementation of IP address filtering function

Source: Internet
Author: User
Tags servervariables
String Proxy,getip;
String Realip;
Realip = request.servervariables["Http_x_forwarded_for"]. ToString ();//return NULL indicates no proxy, otherwise return proxy service IP
Proxy = request.servervariables["REMOTE_ADDR"]. ToString ()/no agent
if (Realip = "")
GetIP = proxy;
Else
GetIP = Realip;
string[] Ip_user_arr = Getip.split ('. ');
Dbreader db = new Dbreader (); Read the IP start and end addresses from the database
DataSet ds = new DataSet ();
DS = db. Select ("SELECT * from Ipcontrol where 1=1");
string[] iPSTAR = new String[ds. Tables[0]. Rows.Count];
string[] Ipend = new String[ds. Tables[0]. Rows.Count];
for (int i = 0; I < ds. Tables[0]. Rows.Count; i++)
{
Ipstar[i] = Sm. Decrypto (ds. Tables[0]. rows[i]["Ipaccessstar"]. ToString ());//Because IP uses the encryption algorithm, it calls SM. Decrpto function
Ipend[i] = Sm. Decrypto (ds. Tables[0]. rows[i]["Ipaccseeend"]. ToString ());
}
if (iPSTAR. Length = = Ipend. Length) && iPSTAR. Length >0)
{
for (int j = 0;j<ipstar. Length; J + +)
{
String ip_start = Ipstar[j];
String ip_end = Ipend[j];
string[] Ip_start_arr = Ip_start. Split ('. ');
string[] Ip_end_arr = ip_end. Split ('. ');
String ip_start_str = "", Ip_end_str = "", Ip_user_str = "";
for (int i = 0;i<4;i++)
{
Ip_start_str + + this.sss (convert.tostring (convert.todouble (ip_start_arr[i))/1000));
Ip_end_str + + this.sss (convert.tostring (convert.todouble (ip_end_arr[i))/1000));
Ip_user_str + + this.sss (convert.tostring (convert.todouble (ip_user_arr[i))/1000));
}
if (convert.todouble (IP_USER_STR) >convert.todouble (ip_end_str) | | Convert.todouble (IP_USER_STR) <convert.todouble (IP_START_STR))
{
Response.Write (' <script language= ' JavaScript ' >alert (' Your IP is not in the Allowed Access IP list. ');</script> ");
}

}
IP meets the criteria when processing
}
Else
{
Processing when the database does not have an IP address filter table
Response.Write (' <script language= ' JavaScript ' >alert (' Your IP is not in the Allowed Access IP list. ');</script> ");

}

Public string sss (string s)
  {
   int i = s.indexof ('. ');
   string temp = s.substring (i+1, s.length-i-1);
   while (temp. Length < 3)
   {
    temp + = "0";
&NBSP;&NBSP;&NBSP}
   return temp;
  .}

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.