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 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";
}