【问题标题】:How to get client's IP address on an Azure Web App developed in ASP.NET?如何在 ASP.NET 开发的 Azure Web App 上获取客户端的 IP 地址?
【发布时间】:2017-10-01 18:32:18
【问题描述】:

我开发了一个 Web 应用程序,它作为 Web 应用程序部署在 Azure 上。

我需要获取客户端的 IP 地址,以便我可以使用 GeoIP API 获取客户端连接的国家/地区。

所以这是我的问题,当他们发送查看主页的请求时,我如何获取客户端的 IP 地址?我正在使用 ASP.NET MVC。

【问题讨论】:

  • Request.ServerVariables["REMOTE_ADDR"] 不起作用?
  • 我试过 System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];但它返回 null 所以 ["HTTP_X_FORWARDED_FOR"]
  • 您可以查看此列表stackoverflow.com/a/6914521/2373249
  • Request.UserHostAddress 呢?

标签: c# asp.net asp.net-mvc azure azure-web-app-service


【解决方案1】:

试试这个(在使用 ASP.NET Core 2.x 的 Azure Web 应用上验证):

using Microsoft.AspNetCore.Http.Features;
using System.Net;

....

var connection = HttpContext.Features.Get<IHttpConnectionFeature>();
IPAddress clientIP = connection.RemoteIpAddress;

【讨论】:

  • IPv4 地址?我认为不是。
  • 这适用于 .net core 3.1
猜你喜欢
  • 1970-01-01
  • 2011-02-04
  • 2020-05-16
  • 2015-04-24
  • 2015-12-20
  • 2016-03-29
  • 2016-10-01
  • 1970-01-01
相关资源
最近更新 更多