asp.net to do IP access restrictions _ Practical tips

Source: Internet
Author: User
Tags connectionstrings

I do a message when I think of doing so, the specific ideas may not be good, to make just a comment, hope that there is a better way!

IP Add page is a ListBox, TextBox, two button, while on other pages directly with the current IP comparison database IP, code as follows!

Restrict IP Add page HTML code

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Ip.aspx.cs" inherits= "Admin_ip"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">
http://www.w3.org/1999/xhtml">
<title> Untitled Page </title>
<link href= ". /images/news.css "rel=" stylesheet "type=" Text/css "/>"
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:sqldatasource id= "SqlDataSource1" runat= "Server" connectionstring= "<%$ Connectionstrings:book"
Providername= "<%$ Connectionstrings:book. ProviderName%> "selectcommand=" SELECT [IP] from [IP] >
</asp:SqlDataSource>
<table align= "center" style= "border-right: #0066cc 1px dotted; Border-top: #0066cc 1px dotted; Border-left: #0066cc 1px dotted; Border-bottom: #0066cc 1px dotted ">
<tr>
&LT;TD rowspan= "2" style= "width:100px; Border-right: #33ccff 1px groove; Border-top: #33ccff 1px groove; Border-left: #33ccff 1px groove; Border-bottom: #33ccff 1px groove; >
<asp:listbox id= "IPLXB" runat= "Server" datasourceid= "SqlDataSource1" datatextfield= "IP"
Datavaluefield= "IP" height= "194px" width= "153px" ></asp:ListBox></td>
&LT;TD style= "width:100px; Border-right: #33ccff 1px groove; Border-top: #33ccff 1px groove; Border-left: #33ccff 1px groove; Border-bottom: #33ccff 1px groove; >
Fill in the standard IP address to the bottom left text box, and then click the button to add! <br/>
<asp:regularexpressionvalidator id= "RegularExpressionValidator1" runat= "Server" controltovalidate= "IPTB"
Display= "Dynamic" errormessage= IP address incorrectly formatted "validationexpression=" ([0-9]{2,3}) ([.]) ([0-9]{1,3}) ([.]) ([0-9]{1,3}) ([.]) ([0-9]{1,3}) "></asp:RegularExpressionValidator></td>
</tr>
<tr>
&LT;TD style= "width:100px; Border-right: #33ccff 1px groove; Border-top: #33ccff 1px groove; Border-left: #33ccff 1px groove; Border-bottom: #33ccff 1px groove; >
<asp:linkbutton id= "LinkButton1" runat= "Server" onclick= "LinkButton1_Click" > Delete selected Rows </asp:LinkButton> </td>
</tr>
<tr>
&LT;TD style= "width:100px; Border-right: #33ccff 1px groove; Border-top: #33ccff 1px groove; Border-left: #33ccff 1px groove; Border-bottom: #33ccff 1px groove; >
<asp:textbox id= "IPTB" runat= "Server" width= "150px" >61.139.33.22</asp:TextBox></td>
&LT;TD style= "width:100px; Border-right: #33ccff 1px groove; Border-top: #33ccff 1px groove; Border-left: #33ccff 1px groove; Border-bottom: #33ccff 1px groove; >
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "Add" width= "80px"/></td>
</tr>
</table>
</div>
</form>
</body>

Limit IP Add page CS code

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
public partial class Admin_ip:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Server.Execute ("chklog.aspx");
}
protected void Button1_Click (object sender, EventArgs e)
{
Iplxb. Items.Add (IPTB. Text);
Odb.insert (INSERT into IP (IP) VALUES (' + IPTB. Text + "')");
}
protected void LinkButton1_Click (object sender, EventArgs e)
{
for (int i = 0; i < IPLXB. Items.Count; i++)
{
if (IPLXB. Items[i]. Selected)
{
Odb.insert ("Delete from IP where ip= '" +IPLXB.) selecteditem.text+ "'");
Iplxb. Items.remove (IPLXB. Selecteditem.text);
}
}
}
}

Code for Page Call page that is required to restrict IP

 protected void Page_Load (object sender, EventArgs e)
    {
         String ip = Request.UserHostAddress.ToString ();
            if (Convert.ToInt32 (ODB.SCR () SELECT count ( * FROM [IP] where ip= ' "+ IP +" ' ") > 0)
            respo Nse. Write ("Sorry, your IP is restricted access, please consult the Administrator");
   }
}

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.