ASP.NET CORE下取IP地址

时间:2024-01-09 10:49:08

先记下来,以后用上了直接来这复制

string ip1 = HttpContext.Request.Headers["X-Real-IP"]; //取IP,NGINX中的配置里要写上
//var feature = HttpContext.Features.Get<IHttpConnectionFeature>();
//string ip2 = feature.RemoteIpAddress.ToString();
//string ip3 = HttpContext.Request.Headers["X-Forwarded-For"];

代码在上面,如果是部署在LINUX上,用NGINX转发的,第二种IP获取方法就取不到了。。取到的只能是像是192.168.1.1这样的内网地址的