String HostName = String. Empty; String IP = string. empty;string IPv4 = string.empty;if (!string. IsNullOrEmpty (system.web.httpcontext.current.request.servervariables["Http_via"])) IP = Convert.ToString ( system.web.httpcontext.current.request.servervariables["Http_x_forwarded_for"]); if (string. IsNullOrEmpty (IP)) IP = request.userhostaddress;//Use the Dns.gethostentry method to reverse Dns records from the acquired IPV6 address,
One by one to determine which is the IPV4 protocol, can be converted to IPV4 address. foreach (IPAddress ipaddr in Dns.gethostentry (IP). AddressList) { if (ipAddr.AddressFamily.ToString () = = "Internetwork") { IPv4 = ipaddr.tostring ();} } HostName = "Host name:" + dns.gethostentry (IP). HostName + "IP:" + IPv4;
In the local program code debugging test, where the string IP will be displayed as:: 1, is the IPV6 IP address format, equivalent to 127.0.0.1. The display address that is eventually processed into IPV4.
ASP. NET MVC gets the host name and IP address of the client computer that accesses the system