[英]Cant Get the IP Address Of the client On my Published Website
我實現了一個簡單的API,該API返回客戶端“請求發送者”的IP地址。 當我不在網絡中時,它總是返回相同的IP地址,而在網絡內部時,它工作得很好,並且返回設備的本地IP地址。 該網站托管在IIS上,其api邏輯如下:
public static class iphelper
{
private const string HttpContext = "MS_HttpContext";
private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
public static string GetClientIpAddress(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey(HttpContext))
{
dynamic ctx = request.Properties[HttpContext];
if (ctx != null)
{
return ctx.Request.UserHostAddress;
}
}
if (request.Properties.ContainsKey(RemoteEndpointMessage))
{
dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
if (remoteEndpoint != null)
{
return remoteEndpoint.Address;
}
}
return null;
}
}
}
我已經嘗試了所有的代碼片段都返回相同的。 我的項目使用ASP.NET MVC C#。
我嘗試過HTTP_X_FORWARDED_FOR總是返回null
它很可能與Web服務器前的負載平衡器或網絡設備有關 。
鑒於:
REMOTE_ADDR重新運行192.168.130.140
我建議您咨詢您的運營團隊,該IP運行的是哪台計算機,以及如何通過調用IP地址(可能通過X-Forwarded-For
)進行傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.