简体   繁体   中英

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM