.net core 获取本地ip及request请求端口

1.获取ip和端口

string str = (Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + Request.HttpContext.Connection.LocalPort);

输出str,会得到当前服务器的IP及端口("127.0.0.1:5001")

2.获取ip

var ip = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
if (string.IsNullOrEmpty(ip))
{
    ip = HttpContext.Connection.RemoteIpAddress.ToString();
}
return ip;  "127.0.0.1"

猜你喜欢

转载自www.cnblogs.com/qingchengshiguang/p/10288481.html