Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 489
EidolonMK Avatar asked Oct 01 '17 18:10

EidolonMK


People also ask

How do I find the IP address of my azure web app?

To find the outbound IP addresses currently used by your app in the Azure portal, click Properties in your app's left-hand navigation. They are listed in the Outbound IP Addresses field.

Can asp net used with Azure?

ASP.NET web apps are cross-platform and can be hosted on Linux or Windows. When you're finished, you'll have an Azure resource group consisting of an App Service hosting plan and an App Service with a deployed web application.

How do I whitelist an IP address in Azure Web App?

However, to configure your IP whitelist for a specific web application, navigate to Settings, Networking, <the Web App overview page>. Under IP restrictions, click Configure IP restrictions.


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;
like image 151
David Ebbo Avatar answered Nov 02 '22 19:11

David Ebbo