.net 做的IP 访问限制

 
  1. 偶做留言本的时候想起做这么个,具体思路也许不好,做出来只是抛砖引玉,希望有更好的方法!
  2. IP添加页是用了一个ListBox, TextBox,两个Button
  3. 而在其他的页上则直接用当前IP对比数据库中的IP,代码如下!
  4. 限制IP添加页HTML代码
  5. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ip.aspx.cs" Inherits="admin_ip" %>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml" >
  8. <head runat="server">
  9.     <title>无标题页</title>
  10.     <link href="../images/news.css" rel="stylesheet" type="text/css" />
  11. </head>
  12. <body>
  13.     <form id="form1" runat="server">
  14.     <div>
  15.         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:book %>"
  16.             ProviderName="<%$ ConnectionStrings:book.ProviderName %>" SelectCommand="SELECT [ip] FROM [ip]">
  17.         </asp:SqlDataSource>
  18.         <table align="center" style="border-right: #0066cc 1px dotted; border-top: #0066cc 1px dotted; border-left: #0066cc 1px dotted; border-bottom: #0066cc 1px dotted">
  19.             <tr>
  20.                 <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;">
  21.                     <asp:ListBox ID="iplxb" runat="server" DataSourceID="SqlDataSource1" DataTextField="ip"
  22.                         DataValueField="ip" Height="194px" Width="153px"></asp:ListBox></td>
  23.                 <td style="width: 100px; border-right: #33ccff 1px groove; border-top: #33ccff 1px groove; border-left: #33ccff 1px groove; border-bottom: #33ccff 1px groove;">
  24.                     填写标准的IP地址到左下文本框里面,然后点击按纽添加!<br />
  25.                     <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="iptb"
  26.                         Display="Dynamic" ErrorMessage="IP地址格式不正确" ValidationExpression="([0-9]{2,3})([.])([0-9]{1,3})([.])([0-9]{1,3})([.])([0-9]{1,3})"></asp:RegularExpressionValidator></td>
  27.             </tr>
  28.             <tr>
  29.                 <td style="width: 100px; border-right: #33ccff 1px groove; border-top: #33ccff 1px groove; border-left: #33ccff 1px groove; border-bottom: #33ccff 1px groove;">
  30.                     <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">删除选中的行</asp:LinkButton></td>
  31.             </tr>
  32.             <tr>
  33.                 <td style="width: 100px; border-right: #33ccff 1px groove; border-top: #33ccff 1px groove; border-left: #33ccff 1px groove; border-bottom: #33ccff 1px groove;">
  34.                     <asp:TextBox ID="iptb" runat="server" Width="150px">61.139.33.22</asp:TextBox></td>
  35.                 <td style="width: 100px; border-right: #33ccff 1px groove; border-top: #33ccff 1px groove; border-left: #33ccff 1px groove; border-bottom: #33ccff 1px groove;">
  36.                     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="增加" Width="80px" /></td>
  37.             </tr>
  38.       </table>
  39.     
  40.     </div>
  41.     </form>
  42. </body>
  43. </html>
  44. 限制IP添加页CS代码
  45. using System;
  46. using System.Data;
  47. using System.Configuration;
  48. using System.Collections;
  49. using System.Web;
  50. using System.Web.Security;
  51. using System.Web.UI;
  52. using System.Web.UI.WebControls;
  53. using System.Web.UI.WebControls.WebParts;
  54. using System.Web.UI.HtmlControls;
  55. public partial class admin_ip : System.Web.UI.Page
  56. {
  57.     protected void Page_Load(object sender, EventArgs e)
  58.     {
  59.         Server.Execute("chklog.aspx");
  60.     }
  61.     protected void Button1_Click(object sender, EventArgs e)
  62.     {
  63.         
  64.         iplxb.Items.Add(iptb.Text);
  65.         odb.insert("insert into ip (ip) values ('" + iptb.Text + "')");
  66.     }
  67.     protected void LinkButton1_Click(object sender, EventArgs e)
  68.     {
  69.         for (int i = 0; i < iplxb.Items.Count; i++)
  70.         {
  71.             if (iplxb.Items[i].Selected)
  72.             {
  73.                 odb.insert("delete from ip where ip='"+iplxb.SelectedItem.Text+"'");
  74.                 iplxb.Items.Remove(iplxb.SelectedItem.Text);
  75.             }
  76.         }
  77.     }
  78. }
  79. 被需要限制IP的页面调用页的代码
  80. protected void Page_Load(object sender, EventArgs e)
  81.     {
  82.         string ip = Request.UserHostAddress.ToString();
  83.             if (Convert.ToInt32(odb.scr("select count(*) from [ip] where ip='" + ip + "'")) > 0)
  84.             Response.Write("对不起,您的IP被限制访问,请咨询管理员");
  85.     }
  86. }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值