如何在用ASP.NET开发的Azure Web应用程序上获取客户端的IP地址?

问题描述:

我已经开发了一个Web应用程序,该应用程序被部署为Azure上的Web应用程序.

I have developed a web application that is deployed as a web app on Azure.

我需要获取客户端的IP地址,以便可以使用GeoIP API来获取客户端连接的国家/地区.

I need to get the client's IP address such that I can use a GeoIP API to get the country from which the client is connecting.

所以这是我的问题,当客户发送请求查看主页时,如何获取客户的IP地址?我正在使用ASP.NET MVC.

So here's my question, how can I get the client's IP address when they send a request to view the homepage? I am using ASP.NET MVC.

尝试一下(在使用ASP.NET Core 2.x的Azure Web App上验证):

Try this (verified on an Azure Web App using ASP.NET Core 2.x):

using Microsoft.AspNetCore.Http.Features;
using System.Net;

....

var connection = HttpContext.Features.Get<IHttpConnectionFeature>();
IPAddress clientIP = connection.RemoteIpAddress;