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: