2020-10-21

C# .Net 判断IP地址是否符合某IP段技巧

在YuebonCore快速开发框架开源项目中涉及到当前登录用户登录IP是否被禁止登录访问系统,获取登录IP后怎么去判断过滤呢?我们采用将IP地址转为Int32数字型,然后去判断大小。

 

Sql sever IP地址转int型

cast(replace(StartIP,'.','') as bigint)

获取当前用户IP地址字符串转int型

int ipv = ip.Replace(".", "").ToInt();

  

综合起来方法如下:

  /// <summary>  /// 验证IP地址是否被拒绝  /// </summary>  /// <param name="ip"></param>  /// <returns></returns>  public bool ValidateIP(string ip)  {   long ipv = ip.Replace(".", "").ToLong();   string where = " (cast(replace(StartIP,'.','') as bigint)>=" + ipv + " and cast(replace(EndIP,'.','') as bigint)<=" + ipv + ") and FilterType=0 and EnabledMark=1";   int count = GetCountByWhere(where);   return count > 0 ? true : false;  }

  

号外:

YuebonCore是基于.NetCore3.1开发的权限管理及快速开发框架

开源地址:https://gitee.com/yuebon/YuebonNetCore

官方文档

原文转载:http://www.shaoqun.com/a/481451.html

retriever:https://www.ikjzd.com/w/773

tchibo:https://www.ikjzd.com/w/1928

3suisses:https://www.ikjzd.com/w/412


在YuebonCore快速开发框架开源项目中涉及到当前登录用户登录IP是否被禁止登录访问系统,获取登录IP后怎么去判断过滤呢?我们采用将IP地址转为Int32数字型,然后去判断大小。SqlseverIP地址转int型cast(replace(StartIP,'.','')asbigint)获取当前用户IP地址字符串转int型intipv=ip.Replace(&qu
慧聪集团:https://www.ikjzd.com/w/1836
photobucket:https://www.ikjzd.com/w/132
2020香港五一放几天假?:http://tour.shaoqun.com/a/45290.html
去澳门旅游要注意那些?:http://tour.shaoqun.com/a/7790.html
一艘中国集装箱船失控!撞上外轮致7个集装箱落入长江!:https://www.ikjzd.com/home/100387

No comments:

Post a Comment