ASP.net IP address access restrictions

Source: Internet
Author: User

I think of this as a message book. The specific ideas may not be good. I just hope there are better methods!

The IP add page uses a ListBox, TextBox, and two buttons. On other pages, the current IP address is used to compare the IP addresses in the database. The Code is as follows!

Restrict IP addresses to add 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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<Link href = "../images/news.css" rel = "stylesheet" type = "text/css"/>
</Head>
<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>
<Td rowspan = "2" style = "width: 100px; border-right: #33 ccff 1px groove; border-top: #33 ccff 1px groove; border-left: #33 ccff 1px groove; border-bottom: #33 ccff 1px groove; ">
<Asp: ListBox ID = "iplxb" runat = "server" DataSourceID = "SqlDataSource1" DataTextField = "ip"
DataValueField = "ip" Height = "194px" Width = "153px"> </asp: ListBox> </td>
<Td style = "width: 100px; border-right: #33 ccff 1px groove; border-top: #33 ccff 1px groove; border-left: #33 ccff 1px groove; border-bottom: #33 ccff 1px groove; ">
Enter the standard IP address in the lower-left text box and click "add! <Br/>
<Asp: RegularExpressionValidator ID = "RegularExpressionValidator1" runat = "server" ControlToValidate = "iptb"
Display = "Dynamic" ErrorMessage = "Incorrect IP address format" ValidationExpression = "([0-9] {2, 3}) ([.]) ([0-9] {1, 3}) ([.]) ([0-9] {1, 3}) ([.]) ([0-9] {1, 3}) "> </asp: RegularExpressionValidator> </td>
</Tr>
<Tr>
<Td style = "width: 100px; border-right: #33 ccff 1px groove; border-top: #33 ccff 1px groove; border-left: #33 ccff 1px groove; border-bottom: #33 ccff 1px groove; ">
<Asp: LinkButton ID = "LinkButton1" runat = "server" OnClick = "LinkButton1_Click"> Delete selected rows </asp: LinkButton> </td>
</Tr>
<Tr>
<Td style = "width: 100px; border-right: #33 ccff 1px groove; border-top: #33 ccff 1px groove; border-left: #33 ccff 1px groove; border-bottom: #33 ccff 1px groove; ">
<Asp: TextBox ID = "iptb" runat = "server" Width = "150px"> 61.139.33.22 </asp: TextBox> </td>
<Td style = "width: 100px; border-right: #33 ccff 1px groove; border-top: #33 ccff 1px groove; border-left: #33 ccff 1px groove; border-bottom: #33 ccff 1px groove; ">
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "add" Width = "80px"/> </td>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

Restrict IP address 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 button#click (object sender, EventArgs e)
{
Iplxb. Items. Add (iptb. Text );
Odb. insert ("insert into ip (ip) values ('" + iptb. Text + "')");
}
Protected void linkbutton#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 of the page call page to be restricted by IP addresses

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)
Response. Write ("sorry, your IP address is restricted, 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.