欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.net做的IP访问限制

程序员文章站 2023-08-17 12:03:42
  偶做留言本的时候想起做这么个,具体思路也许不好,做出来只是抛砖引玉,希望有更好的方法!  ip添加页是用了一个listbox, textbox,两个button,而在其...

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

  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="" >
<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" 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: #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>
                <td style="width: 100px; border-right: #33ccff 1px groove; border-top: #33ccff 1px groove; border-left: #33ccff 1px groove; border-bottom: #33ccff 1px groove;">
                    填写标准的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 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">删除选中的行</asp:linkbutton></td>
            </tr>
            <tr>
                <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>
                <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="增加" 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被限制访问,请咨询管理员");
    }
}