1、需要手动的在Nuget中引入 "Microsoft.AspNetCore.HttpOverrides" 这个包;
2、在我们的Startup.cs的 Configure方法里加上:
app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto });
3、添加引用:
using Microsoft.AspNetCore.HttpOverrides
4、获取客户端IP地址:
Request.HttpContext.Connection.RemoteIpAddress;
5、获取客户端访问端口号:
Request.HttpContext.Connection.RemotePort;