首页 > 编程 > .NET > 正文

ASP.net 做的IP 访问限制

2024-07-10 13:10:22
字体:
来源:转载
供稿:网友

  偶做留言本的时候想起做这么个,具体思路也许不好,做出来只是抛砖引玉,希望有更好的方法!

  ip添加页是用了一个listbox, textbox,两个button,而在其他的页上则直接用当前ip对比数据库中的ip,代码如下!

  限制ip添加页html代码

<%@ 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>无标题页</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" >
            <tr>
                <td rowspan="2" >
                    <asp:listbox id="iplxb" runat="server" datasourceid="sqldatasource1" datatextfield="ip"
                        datavaluefield="ip" height="194px" width="153px"></asp:listbox></td>
                <td >
                    填写标准的ip地址到左下文本框里面,然后点击按纽添加!<br />
                    <asp:regularexpressionvalidator id="regularexpressionvalidator1" runat="server" controltovalidate="iptb"
                        display="dynamic" errormessage="ip地址格式不正确" validationexpression="([0-9]{2,3})([.])([0-9]{1,3})([.])([0-9]{1,3})([.])([0-9]{1,3})"></asp:regularexpressionvalidator></td>
            </tr>
            <tr>
                <td >
                    <asp:linkbutton id="linkbutton1" runat="server" onclick="linkbutton1_click">删除选中的行</asp:linkbutton></td>
            </tr>
            <tr>
                <td >
                    <asp:textbox id="iptb" runat="server" width="150px">61.139.33.22</asp:textbox></td>
                <td >
                    <asp:button id="button1" runat="server" onclick="button1_click" text="增加" width="80px" /></td>
            </tr>
      </table>   
    </div>
    </form>
</body>
</html>

  限制ip添加页cs代码

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);
            }
        }
    }
}

  被需要限制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)
            response.write("对不起,您的ip被限制访问,请咨询管理员");
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表