问题
I have developed a web application that is deployed as a web app on Azure.
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.
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.
回答1:
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;
来源:https://stackoverflow.com/questions/46515568/how-to-get-clients-ip-address-on-an-azure-web-app-developed-in-asp-net