【问题标题】:Getting client IP address in dotnet core web api ::ffff:127.0.0.1在 dotnet core web api ::ffff:127.0.0.1 中获取客户端 IP 地址
【发布时间】:2020-12-09 10:23:37
【问题描述】:

我正在尝试在我的 dotnet core web api 中获取客户端(web api 的调用者)IP 地址。我从不同的客户端网络测试 IP 地址为“::ffff:127.0.0.1”。本地主机获取为“::::1” 我在我的操作方法中使用以下代码:

var ipaddress = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress?.ToString();

也尝试过以下代码:

var ipaddress  = Request.HttpContext.Connection.RemoteIpAddress;

得到相同的结果。我们如何在 dotnet core web api 中获取准确的 IP 地址?

【问题讨论】:

标签: asp.net-core-webapi webapi


【解决方案1】:

看起来您将应用程序托管在 IIS 上,位于防火墙、NAT、Apache 路由等之后。请检查您在请求中获得的标头列表。如果请求已被转发,IP 地址可能是其中之一。您可能还想看看这个答案:https://*.com/a/62060533/9890829

【讨论】: