客户的外部IP

时间:2014-12-11 13:38:01

标签: c# visual-studio-2012 model-view-controller ip .net-4.5

我使用MVC4,.NET 4.5,VS2012,C#,Razor

我需要在我的网站上提供客户的公共IP。为了澄清,我需要whatismyip显示的那种IP。 我知道查询whatismyip的自动化页面。但是,我需要自己获取IP而不是使用其他网站。以下是我目前的代码。

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Web.Routing;
using System.Net.Sockets;
using System.Text;

namespace abc
{
    public class GetIP : Controller
    {
        public ActionResult GetIP()
        {
            ViewBag.ip1 = string.Empty;
            ViewBag.ip2 = string.Empty;
            ViewBag.defaultgateway = string.Empty;
            ViewBag.ip4 = string.Empty;
            ViewBag.dnsServer = string.Empty;
            ViewBag.ip6 = string.Empty;
            ViewBag.server_IPv6_address = string.Empty;

            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] != null)
                ViewBag.ip1 = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];

            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                ViewBag.ip2 = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

            if (System.Web.HttpContext.Current.Request.UserHostAddress.Length != 0)
                ViewBag.defaultgateway = System.Web.HttpContext.Current.Request.UserHostAddress;

            if (System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
                ViewBag.ip4 = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

            if (System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"] != null)
                ViewBag.dnsServer = System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"].ToString();

            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"] != null)
                ViewBag.ip6 = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_CLUSTER_CLIENT_IP"].ToString();

            ViewBag.server_IPv6_address = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();

            if (Request.ServerVariables["X-Forwarded-For"] != null)
                ViewBag.ip8 = Request.ServerVariables["X-Forwarded-For"];

            return View();
        }
    }
}

查看

Your IP address<br />
Ip1 : @ViewBag.ip1<br />
Ip2 : @ViewBag.ip2<br />
Default Gateway : @ViewBag.defaultgateway<br />
Ip4 : @ViewBag.ip4<br />
DNS Server : @ViewBag.dnsServer<br />
IP6 : @ViewBag.ip6<br />
@*Server's IPv6 Address : @ViewBag.server_IPv6_address*@
IP8 : @ViewBag.ip8<br />

当我将此代码放在服务器上并转到相应的URL时,我得到以下结果。

enter image description here

当我去whatismyip时,说我得到123.45.67.89。 为什么whatismyip可以找到我的IP地址,但我的服务器可以使用我检查过的所有标头。我也读过有关System.Net.Sockets课程的某个地方,但我不知道这是否会有所帮助。 请帮忙。

编辑:显然这是服务器配置的问题。当我将相同的代码放在不同的服务器上时,它完美地运行。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

您没有看到外部IP,因为您通过内部网络访问与您在同一网络中的服务器。

如果您通过智能手机(而非通过WiFi)或通过外部代理访问该网站,您将看到(移动网络或代理服务器)的外部IP。

答案 1 :(得分:0)

System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"]是服务器用于响应您的请求的地址。它不是DNS服务器,正如您的代码所暗示的那样。

该值“192.168.4.202”是服务器正在侦听并用于响应您的请求的地址。如果这是您从互联网连接时收到的结果,则表示您的Web服务器位于某种负载均衡器或过滤器代理之后。

来自该计算机的传出连接可能采用完全不同的路径。您可能位于传入连接的负载平衡器后面,但不适用于传出连接。 yourismyip.com中的IP地址可能与用于连接到您网站的IP地址完全不同。

找到公共IP的最佳选择是使用DNS查找网站的主机名。这将告诉您任何远程计算机(智能手机,家用计算机等)将用于连接到该网站的IP地址。您还可以与负责设置网络部分的技术人员交谈。

相关问题