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

不羁岁月 提交于 2019-12-06 02:13:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!