博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
根绝ip限制访问
阅读量:6914 次
发布时间:2019-06-27

本文共 1733 字,大约阅读时间需要 5 分钟。

       今天有个客户要求在内网里限制下访问,根据ip端,自己就粗略写了一些,方法比较笨,不过很实用,代码如下:

 

    
//
限制ip段访问
    
public 
static 
bool CheckIp()
    {
        
bool fig = 
false;
        
//
首先获得客户端ip
        
string clientIp = GetIP();
        
if (clientIp.Substring(
0, clientIp.LastIndexOf(
"
.
")) == 
"
10.57.70
")
        {
            
//
判断最后一位数字的范围
            
int lastNum = Convert.ToInt32(clientIp.Substring(clientIp.LastIndexOf(
"
.
") + 
1));
            
if (lastNum <= 
255 && lastNum > 
0)
            {
                fig = 
true;
            }
        }
        
else 
if (clientIp.Substring(
0, clientIp.LastIndexOf(
"
.
")) == 
"
10.57.74
")
        {
            
int lastNum = Convert.ToInt32(clientIp.Substring(clientIp.LastIndexOf(
"
.
") + 
1));
            
if (lastNum <= 
191 && lastNum >= 
0)
            {
                fig = 
true;
            }
        }
        
else 
if (clientIp.Substring(
0, clientIp.LastIndexOf(
"
.
")) == 
"
10.57.75
")
        {
            
int lastNum = Convert.ToInt32(clientIp.Substring(clientIp.LastIndexOf(
"
.
") + 
1));
            
if (lastNum <= 
63 && lastNum >= 
0)
            {
                fig = 
true;
            }
        }
        
else 
if (clientIp.Substring(
0, clientIp.LastIndexOf(
"
.
")) == 
"
10.57.76
")
        {
            
int lastNum = Convert.ToInt32(clientIp.Substring(clientIp.LastIndexOf(
"
.
") + 
1));
            
if (lastNum <= 
63 && lastNum >= 
0)
            {
                fig = 
true;
            }
        }
        
else 
if (clientIp.Substring(
0, clientIp.LastIndexOf(
"
.
")) == 
"
10.57.74
")
        {
            
int lastNum = Convert.ToInt32(clientIp.Substring(clientIp.LastIndexOf(
"
.
") + 
1));
            
if (lastNum <= 
191 && lastNum >= 
128)
            {
                fig = 
true;
            }
        }
        
return fig;
    }
    
public 
static 
string GetIP()
    {
        
//
 优先取得代理IP
        
string userHostAddress = HttpContext.Current.Request.ServerVariables[
"
HTTP_X_FORWARDED_FOR
"];
        
if (
string.IsNullOrEmpty(userHostAddress))
        {
            
//
没有代理IP则直接取客户端IP
            userHostAddress = HttpContext.Current.Request.ServerVariables[
"
REMOTE_ADDR
"];
        }
        
if ((userHostAddress != 
null) && !(userHostAddress == 
string.Empty))
        {
            
return userHostAddress;
        }
        
return 
"
0.0.0.0
";
    }

 然后在限制的页面里调用这段代码即可,呵呵,时间关系,没考虑太多,大家可以在简便一些,思路大概就是这样!

转载地址:http://sfacl.baihongyu.com/

你可能感兴趣的文章
kingshard架构设计和功能实现
查看>>
SQL Server 移动数据库
查看>>
错误:ORA-01861: 文字与格式字符串不匹配
查看>>
美国IXWebHosting虚拟主机助力外贸企业 成就营销新热点
查看>>
ftp服务器搭建(一)
查看>>
使用本地mail发送外网邮件
查看>>
我的友情链接
查看>>
Struts ActionForm的优化写法(转)
查看>>
Linux 下三个密码生成工具
查看>>
Nagios利用NSClient++监控Windows主机
查看>>
UltraISO(软碟通)制作安装Ubuntu系统的U盘安装盘
查看>>
最有趣的推理
查看>>
集合框架-Map
查看>>
python读取和生成excel文件
查看>>
我的友情链接
查看>>
tcpdump抓包 wireshark(ethereal)分析
查看>>
PHP 的 wordwrap 扩展功能
查看>>
ipsec的简单介绍及应用
查看>>
基于Eclipse的Hadoop应用开发环境配置
查看>>
分布式搜索方案选型
查看>>