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

 

大家對此問題有什么好的方法呢?評論區討論。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM