欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-14 18:06:06
1.获取ip和端口 string str = (Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + Request.HttpContext.Connection.LocalPort); 输出str, ......

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"