From 50ce20071a18c97c7a2813c995f93e4c131fd520 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Thu, 2 Nov 2023 15:53:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=B9=E5=99=A8=E7=9C=9F=E5=AE=9Eip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.Infra.EFCore/AuthUser/UserInfo.cs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/IRaCIS.Core.Infra.EFCore/AuthUser/UserInfo.cs b/IRaCIS.Core.Infra.EFCore/AuthUser/UserInfo.cs index 890033cda..eddead0fd 100644 --- a/IRaCIS.Core.Infra.EFCore/AuthUser/UserInfo.cs +++ b/IRaCIS.Core.Infra.EFCore/AuthUser/UserInfo.cs @@ -212,7 +212,7 @@ namespace IRaCIS.Core.Domain.Share get { - return _accessor?.HttpContext?.Connection.RemoteIpAddress.ToString(); + return _accessor?.HttpContext.GetClientIP(); } } @@ -341,4 +341,33 @@ namespace IRaCIS.Core.Domain.Share } + + public static class HttpContextExtension + { + public static string GetClientIP(this HttpContext context) + { + var ip = context.Request.Headers["Cdn-Src-Ip"].FirstOrDefault(); + if (!string.IsNullOrEmpty(ip)) + return IpReplace(ip); + + ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault(); + if (!string.IsNullOrEmpty(ip)) + return IpReplace(ip); + + ip = context.Connection.RemoteIpAddress.ToString(); + + return IpReplace(ip); + } + + static string IpReplace(string inip) + { + //::ffff: + //::ffff:192.168.2.131 这种IP处理 + if (inip.Contains("::ffff:")) + { + inip = inip.Replace("::ffff:", ""); + } + return inip; + } + } }