ASP. NET MVC gets the host name and IP address of the client computer that accesses the system

Source: Internet
Author: User
Tags servervariables reverse dns

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.