VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C# .Net 判断IP地址是否符合某IP段技巧

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

 

Sql sever IP地址转int型

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

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

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

  

综合起来方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
/// <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

 

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

 

本文链接:https://www.cnblogs.com/cqinwn/p/13854221.html


相关教程