MVC4, .NET 4.5, VS2012, C#, 면도기클라이언트의 외부 IP
내 웹 사이트에서 클라이언트의 공개 IP가 필요합니다. 명확히하기 위해 나는 whatismyip이 보여주는 종류의 지적 재산권이 필요하다. 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();
}
}
}
보기 I는 해당 URL을 서버에이 코드를 넣어 이동
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 />
, 나는 다음과 같은 결과를 얻을. 내가 whatismyip에 갈 때
는, 나는 예를 들어 123.45.67.89 얻을 말한다. 왜 whatismyip이 내 IP 주소를 찾을 수 있지만 내 서버는 내가 확인한 모든 헤더를 사용할 수 없습니다. 나도 어딘가에 대해 System.Net.Sockets
클래스를 읽었다. 그러나 그것이 도움이되는지 또는 무엇이되는지 나는 모른다. 도와주세요.
편집 : 분명히 이것은 서버 구성에 문제가 있습니다. 동일한 코드를 다른 서버에 넣으면 완벽하게 작동합니다. 도와 줘서 고마워!
예, 스마트 폰에서 사용해 보았습니다. 이상하게 결과가 같았습니다. – Adze
그런 다음 WiFi를 끄지 않았습니다. 즉, 전화기가 서버와 동일한 내부 네트워크에 있음을 의미합니다. – CodeCaster
아니요, Wi-Fi를 끈 상태로 휴대 전화의 3G 인터넷을 사용했습니다. – Adze