.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"

posted @ 2019-01-18 16:47  青橙时光  阅读(4701)  评论(0编辑  收藏  举报