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://docs.v.yuebon.com

 

大家对此问题有什么好的方法呢?评论区讨论。

posted @ 2020-10-21 19:34  cqinwn  阅读(775)  评论(0编辑  收藏  举报