天天看點

C# .Net 判斷IP位址是否符合某IP段技巧

在YuebonCore快速開發架構開源項目中涉及到目前登入使用者登入IP是否被禁止登入通路系統,擷取登入IP後怎麼c#教程去判斷過濾呢?我們采用将IP位址轉為Int32數字型,然後去判斷大小。

Sql sever IP位址轉int型

擷取目前使用者IP位址字元串轉int型

綜合起來方法如下:

/// <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

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