我想從ASP.NET MVC 5.用戶的IP地址相同的內部網絡地址,我擡頭一看各種例子,像這樣的:客戶端IP地址返回
- https://stackoverflow.com/a/740431/177416
- https://stackoverflow.com/a/20194511/177416
- https://stackoverflow.com/a/3003254/177416
他們都產生相同的結果:用戶被視爲內部網絡。我有朋友試過他們的手機(這不在網絡上)。這裏是我的最新嘗試:
private static Logger _logger = LogManager.GetCurrentClassLogger();
public static bool IsIpInternal()
{
var ipAddress = HttpContext.Current.Request.UserHostAddress;
var logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, ipAddress);
_logger.Log(logEvent);
try
{
if (ipAddress != null)
{
var ipParts = ipAddress.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse).ToArray();
var isDebug = System.Diagnostics.Debugger.IsAttached;
if (ipParts[0] == 10)
{
return true;
}
}
}
catch (Exception e)
{
logEvent = new LogEventInfo(LogLevel.Error, _logger.Name, e.Message);
_logger.Log(logEvent);
return false;
}
return false;
}
的日誌中顯示10.xxx.xx.xxx
所有請求(基於日誌)。這是一個內部地址,而不是連接到Web應用程序的客戶端的IP地址。 IsIpInternal()
總是返回true。我究竟做錯了什麼?
請注意,我忽略了192.168.x.x
和172.16.xxx.xxx
地址爲內部地址。
您的網站是代理還是負載均衡器? –
@JohnWu,是的,我們有一臺Citrix NetScaler,所有服務器都使用它。那可以刪除原來的IP地址嗎? – Alex