ASP. NET 6 methods for obtaining IP addresses (reproduced in LanceZhang & #39; s Tech Blog), asp. netlancezhang

Source: Internet
Author: User

ASP. NET 6 methods for obtaining IP addresses (reproduced in LanceZhang's Tech Blog), asp. netlancezhang

Server:

1 // method 2 HttpContext. current. request. userHostAddress; 3 4 // method 2 5 HttpContext. current. request. serverVariables ["REMOTE_ADDR"]; 6 7 // method 3 8 string strHostName = System. net. dns. getHostName (); 9 string clientIPAddress = System. net. dns. getHostAddresses (strHostName ). getValue (0 ). toString (); 10 11 // Method 4 (ignore proxy) 12 HttpContext. current. request. serverVariables ["HTTP_X_FORWARDED_FOR"];

 

Client:

1 // Method 5 2 var ip = '<! -- # Echo var = "REMOTE_ADDR" --> '; 3 alert ("Your IP address is" + ip); 4 5 6 7 // Method 6 (ignore proxy) 8 function GetLocalIPAddress () 9 {10 var obj = null; 11 var rslt = ""; 12 try 13 {14 obj = new ActiveXObject ("rcbdyctl. setting "); 15 rslt = obj. getIPAddress; 16 obj = null; 17} 18 catch (e) 19 {20 // 21} 22 return rslt; 23}

 

MCT Maulik Patel from India provides a server solution:

1 if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy2 { 3      ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.4 }5 else// not using proxy or can't get the Client IP6 { 7      ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.9 }

 

Note:




Related Article

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.