.net 做的IP 访问限制

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

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" 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被限制访问,请咨询管理员");
    }
}

限制Elasticsearch的IP访问,有几种方法可供选择。首先,可以使用IP tables来限制访问。根据引用中的参考文档,你可以通过配置IP tables规则来允许或拒绝特定的IP地址访问Elasticsearch。这是一种简单有效的方法,但需要在服务器上进行配置。 另一种方法是使用Elasticsearch的插件xpack,如引用所述。xpack是一个收费插件,提供了一套安全功能,包括身份验证和访问控制。通过xpack,你可以配置IP白名单,只允许特定的IP地址访问Elasticsearch。这样可以更细粒度地控制访问权限,但需要购买和安装xpack插件。 第三种方法是使用网络相关的参数来限制Elasticsearch的访问。引用提到了一种方法,即通过配置network.host,network.bind_host,network.publish_host这三个参数来限制访问IP。然而,这种方法可能无法满足你的需求,因为它不能提供细粒度的IP控制。 综上所述,限制Elasticsearch的IP访问可以通过配置IP tables规则、使用xpack插件或通过设置网络相关参数来实现。你可以根据自己的具体需求和预算选择适合的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Elasticsearch限制IP访问](https://blog.csdn.net/qq_18746961/article/details/124193303)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值