【问题标题】:How do I get client IP address in ASP.NET CORE 3.1?如何在 ASP.NET CORE 3.1 中获取客户端 IP 地址?
【发布时间】:2020-04-29 21:06:39
【问题描述】:

在 Web API 项目中编写代码时如何在 ASP.NET 3.1 中获取客户端 IP 地址

【问题讨论】:

标签: asp.net-core asp.net-core-webapi asp.net-core-3.1


【解决方案1】:

使用本教程。希望它会帮助你。 Get client IP address in Asp.NET core 3

或者只是在你的控制器中使用这行代码:

var ip =HttpContext.Connection.RemoteIpAddress.ToString()

【讨论】:

  • 链接失效了!
【解决方案2】:
string ipAddress = HttpContext.Connection.RemoteIpAddress.ToString(); 

deviceName = Dns.GetHostEntry(HttpContext.Connection.RemoteIpAddress).HostName;

【讨论】: