容器真实ip

Uat_Study
hang 2023-11-02 15:53:09 +08:00
parent 419d5de7f1
commit 99439a4809
1 changed files with 30 additions and 1 deletions

View File

@ -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;
}
}
}