最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Request.ServerVariables["remote_addr"] is not workin in Edge - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 0

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

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;
}
发布评论

评论列表(0)

  1. 暂无评论