9

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.

EidolonMK
  • 313
  • 2
  • 9

1 Answers1

9

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;
David Ebbo
  • 42,443
  • 8
  • 103
  • 117