iotgateway/WalkingTec.Mvvm/WalkingTec.Mvvm.Mvc/Helper/HttpContextExtention.cs

28 lines
685 B
C#
Raw Normal View History

2021-12-14 06:10:44 +00:00
using Microsoft.AspNetCore.Http;
using System.Linq;
namespace WalkingTec.Mvvm.Mvc
{
public static class HttpContextExtention
{
/// <summary>
/// 客户端 Ip 头
/// </summary>
public const string REMOTE_IP_HEADER = "X-Forwarded-For";
public static string GetRemoteIpAddress(this HttpContext self)
{
var proxyIp = self.Request?.Headers?[REMOTE_IP_HEADER].FirstOrDefault();
if (!string.IsNullOrEmpty(proxyIp))
{
return proxyIp;
}
else
{
return self.Connection.RemoteIpAddress.ToString();
}
}
}
}