目前采用的获取方法
var ip = HttpContext.Connection.LocalIpAddress.MapToIPv4()?.ToString();
var hostIps = NetworkInterface
.GetAllNetworkInterfaces()
.Where(network => network.OperationalStatus == OperationalStatus.Up)
.Select(network => network.GetIPProperties())
.OrderByDescending(properties => properties.GatewayAddresses.Count)
.SelectMany(properties => properties.UnicastAddresses)
.Where(address => !IPAddress.IsLoopback(address.Address) && address.Address.AddressFamily == AddressFamily.InterNetwork)
.ToArray();
这个太复杂了
@dudu: 使用场景不一样把,比如没有http请求的时候,怎么来的HttpContext.
获取真实ip
1.添加Unicorn程序包
2.修改Program.cs,添加.UseRealIp("X-Forwarded-For"); //真实ip
3.HttpContext.Connection.RemoteIpAddress.ToString()
不是客户端的 IP 地址,是服务器自己的 IP 地址