I'm using ASP.NET 4.8.
Request.ServerVariables["remote_addr"]
is not working in Chromium-based browsers like Edge and Chrome. However, the same code is working absolutely fine in Firefox.
Here is the code:
string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(ipAddress) ||
ipAddress.ToLower() == "unknown" ||
ipAddress == "::1")
ipAddress = Request.UserHostAddress;
This code returns "127.0.0.1" in Firefox, but the same code returns "::1" in Edge and Chrome.
This seems to be a Chromium-based browser specific issue.
What do you think? Is this a known issue? Is there any alternative code for Edge and Chrome browsers?
I'm using ASP.NET 4.8.
Request.ServerVariables["remote_addr"]
is not working in Chromium-based browsers like Edge and Chrome. However, the same code is working absolutely fine in Firefox.
Here is the code:
string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(ipAddress) ||
ipAddress.ToLower() == "unknown" ||
ipAddress == "::1")
ipAddress = Request.UserHostAddress;
This code returns "127.0.0.1" in Firefox, but the same code returns "::1" in Edge and Chrome.
This seems to be a Chromium-based browser specific issue.
What do you think? Is this a known issue? Is there any alternative code for Edge and Chrome browsers?
Share Improve this question edited Feb 3 at 13:46 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 3 at 12:00 Satyajit PrakashSatyajit Prakash 32 bronze badges2 Answers
Reset to default 0I think it is not a Chromium-specific "problem", as IE also shows ::1
as the IP address. ::1
simply means LOCALHOST
in IPv6. It looks like Firefox has some mechanism to show IPv4 instead. You can check your network adapter settings, and try disabling IPv6 for a test.
Anyway, if you need to return 127.0.0.1
on every single browser, you might as well treat ::1
as 127.0.0.1
:
if (ipAddress == "::1")
{
ipAddress = "127.0.0.1";
}
You should use the X-Forwarded-For
string ipAddress = Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(ipAddress) || ipAddress.ToLower() == "unknown" || ipAddress == "::1")
{
ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress) || ipAddress.ToLower() == "unknown")
{
ipAddress = Request.UserHostAddress;
}
}
if (string.IsNullOrEmpty(ipAddress) || ipAddress.ToLower() == "unknown")
{
ipAddress = Request.UserHostAddress;
}