From 8c7d3e60f6de790d5444e5b4f1edd556bef28075 Mon Sep 17 00:00:00 2001 From: qian Date: Mon, 13 Nov 2023 14:38:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=99=BB=E5=BD=95=E8=A1=A8?= =?UTF-8?q?=20=E6=9A=82=E6=9C=AA=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/AppBoxEntity/AppBoxContext.cs | 1 + 北京北汽/Models/CK.SCP.Models.csproj | 1 + 北京北汽/SCP/Business/PageBase.cs | 112 ++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/北京北汽/Models/AppBoxEntity/AppBoxContext.cs b/北京北汽/Models/AppBoxEntity/AppBoxContext.cs index 65dfad3..034067e 100644 --- a/北京北汽/Models/AppBoxEntity/AppBoxContext.cs +++ b/北京北汽/Models/AppBoxEntity/AppBoxContext.cs @@ -20,6 +20,7 @@ namespace CK.SCP.Models.AppBoxEntity public DbSet Roles { get; set; } public DbSet Titles { get; set; } public DbSet<Online> Onlines { get; set; } + public DbSet<LoginTable> LoginTable{ get; set; } public DbSet<Log> Logs { get; set; } public DbSet<Power> Powers { get; set; } public DbSet<Menu> Menus { get; set; } diff --git a/北京北汽/Models/CK.SCP.Models.csproj b/北京北汽/Models/CK.SCP.Models.csproj index 6b7e46e..1ae635f 100644 --- a/北京北汽/Models/CK.SCP.Models.csproj +++ b/北京北汽/Models/CK.SCP.Models.csproj @@ -61,6 +61,7 @@ <Compile Include="AppBoxEntity\Config.cs" /> <Compile Include="AppBoxEntity\Dept.cs" /> <Compile Include="AppBoxEntity\ExcelImportEntity\APPBOX_USER_EXCEL.cs" /> + <Compile Include="AppBoxEntity\LoginTable.cs" /> <Compile Include="AppBoxEntity\RoleUsers.cs" /> <Compile Include="AppBoxEntity\TA_ADDRESS.cs" /> <Compile Include="AppBoxEntity\ICustomTree.cs" /> diff --git a/北京北汽/SCP/Business/PageBase.cs b/北京北汽/SCP/Business/PageBase.cs index a66d9b1..8ba1820 100644 --- a/北京北汽/SCP/Business/PageBase.cs +++ b/北京北汽/SCP/Business/PageBase.cs @@ -31,6 +31,7 @@ using CK.SCP.Models.Enums; using SCP.Common; using System.Drawing.Imaging; using System.Drawing; +using System.Net.NetworkInformation; namespace SCP { @@ -1833,5 +1834,116 @@ namespace SCP } } + #region 获取反向代理时的客户端的IP地址 getClientIP + /// <summary> + /// 获取反向代理时的客户端的IP地址 + /// </summary> + /// <returns>返回客户端真实IP</returns> + private string getClientIP() + { + HttpRequest request = HttpContext.Current.Request; + + string ip = request.Headers.Get("x-forwarded-for"); + + if (ip == null || ip.Length == 0 || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase)) + { + ip = request.Headers.Get("Proxy-Client-IP"); + } + if (ip == null || ip.Length == 0 || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase)) + { + ip = request.Headers.Get("WL-Proxy-Client-IP"); + + } + if (ip == null || ip.Length == 0 || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase)) + { + ip = request.UserHostAddress; + } + //可能存在如下格式:X-Forwarded-For: client, proxy1, proxy2 + int i = 0; + if (ip.Contains(", ")) + { + //如果存在多个反向代理,获得的IP是一个用逗号分隔的IP集合,取第一个 + //X-Forwarded-For: client 第一个 + string[] ipaddrs = ip.Split(new string[1] { ", " }, StringSplitOptions.RemoveEmptyEntries); + + for (i = 0; i < ipaddrs.Length; i++) + { + if (ipaddrs[i] != "") + { + if (false == IsInnerIP(ipaddrs[i]))//判断是否为内网IP + { + IPAddress realip; + if (IPAddress.TryParse(ipaddrs[i], out realip) && ipaddrs[i].Split('.').Length == 4) + {//合法IP + return ipaddrs[i]; + } + else + {//非法IP + //IP地址不符合规范 + } + } + } + } + ip = ipaddrs[0];//默认取第一个ip地址 + } + + return ip; + } + #endregion + #region 判断IP地址是否为局域网内网地址 + /// <summary> + /// 判断IP地址是否为内网IP地址 + /// </summary> + /// <param name="ipAddress">IP地址字符串</param> + /// <returns></returns> + private bool IsInnerIP(String ipAddress) + { + bool isInnerIp = false; + ulong ipNum = ip2ulong(ipAddress); + /** + 私有IP:A类 10.0.0.0-10.255.255.255 + B类 172.16.0.0-172.31.255.255 + C类 192.168.0.0-192.168.255.255 + 当然,还有127这个网段是环回地址 + **/ + ulong aBegin = ip2ulong("10.0.0.0"); + ulong aEnd = ip2ulong("10.255.255.255"); + ulong bBegin = ip2ulong("172.16.0.0"); + ulong bEnd = ip2ulong("172.31.255.255"); + ulong cBegin = ip2ulong("192.168.0.0"); + ulong cEnd = ip2ulong("192.168.255.255"); + isInnerIp = IsInner(ipNum, aBegin, aEnd) || IsInner(ipNum, bBegin, bEnd) || IsInner(ipNum, cBegin, cEnd) || ipAddress.Equals("127.0.0.1"); + return isInnerIp; + } + /// <summary> + /// 把IP地址转换为Long型数字 + /// </summary> + /// <param name="ipAddress">IP地址字符串</param> + /// <returns></returns> + private ulong ip2ulong(string ipAddress) + { + byte[] bytes = IPAddress.Parse(ipAddress).GetAddressBytes(); + ulong ret = 0; + + foreach (byte b in bytes) + { + ret <<= 8; + ret |= b; + } + return ret; + } + /// <summary> + /// 判断用户IP地址转换为Long型后是否在内网IP地址所在范围 + /// </summary> + /// <param name="userIp"></param> + /// <param name="begin"></param> + /// <param name="end"></param> + /// <returns></returns> + private bool IsInner(ulong userIp, ulong begin, ulong end) + { + return (userIp >= begin) && (userIp <= end); + } + #endregion + } } \ No newline at end of file