如何获得用户的公网IP​​服务器是否在路由器后面? [英] How to get user's public IP if the server is behind a router?

查看:232
本文介绍了如何获得用户的公网IP​​服务器是否在路由器后面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得用户的公网IP​​,如果我的服务器是在路由器后面?

How can I get the user's public IP if my server is behind a router?

我想:

protected string GetIPAddress() 
{
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }
    return context.Request.ServerVariables["REMOTE_ADDR"]; 
}

但我得到的是路由器的网关。

but all I get is the router gateway.

显然, REMOTE_ADDR 不会在我的情况也不会 HTTP_X_FORWARDED_FOR 工作,。

Apparently, REMOTE_ADDR won't work in my case and neither will HTTP_X_FORWARDED_FOR.

如何在我的情况下,得到了用户的公网IP​​任何想法?

Any ideas on how to get the user's public IP in my case?

感谢。

推荐答案

有没有得到它的方式。

但是,可以发现用户是否从本地网络或从网络打开该网页。

But, you can find whether the user is opening the page from a local network or from the web.

如果该IP是路由器分配给从网络传来的请求的IP,那么很显然它来自网上。

If the IP is the IP that the router assigns to the requests coming from the web, then it's clear it comes from the web.

这篇关于如何获得用户的公网IP​​服务器是否在路由器后面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆