How to get client's IP address on an Azure Web App developed in ASP.NET?

Viewed 4441

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 Answers

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;
Related