How to get an IP address?

I want to get the ip address that is registered on my site. How to do it in ASPNET. I used the following code, but it does not get the correct IP address

string ipaddress = Request.UserHostAddress; 
+48
Dec 15 '09 at 12:45
source share
7 answers

This method can be used to obtain the IP address of the client machine.

 public static String GetIP() { String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } return ip; } 
+61
Dec 15 '09 at 12:46
source share

In a situation where you use an IP address for security, you should be aware of your infrastructure.

If you use a proxy between your web server and your clients, which sets the header, you should be able to trust the last address. Then you use the code, for example, proposed by Muhammad with the update, to always get the latest IP address from the direct header (see Code below).

If you are not using a proxy server, be careful that the X-Forwarded-For header is very easy to trick. I suggest you ignore this if you have no clear reason why not.

I updated the code of Muhammad Akhtar as follows so that you can choose:

 public string GetIP(bool CheckForward = false) { string ip = null; if (CheckForward) { ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; } if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } else { // Using X-Forwarded-For last address ip = ip.Split(',') .Last() .Trim(); } return ip; } 

This Wikipedia article explains the risks more thoroughly.

+36
Nov 06
source share

You must use HTTP_X_FORWARDED_FOR, but it can return multiple comma separated IPs. See this page .

Therefore, you should always check it. I personally use the Split function.

 public static String GetIPAddress() { String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(ip)) ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; else ip = ip.Split(',')[0]; return ip; } 
+34
Dec 17 '09 at 18:53
source share

In MVC 6, you get the IP address as follows:

 HttpContext.Request.HttpContext.Connection.RemoteIpAddress.ToString() 
+5
Jul 03 '17 at 16:06
source share

If the client connects through a transparent non-anonymous proxy, you can get your address:

 Request.ServerVariables["HTTP_X_FORWARDED_FOR"] 

which can return null or "unknown" if the IP address cannot be obtained this way.

Request.ServerVariables["REMOTE_ADDR"] must be the same as Request.UserHostAddress , any of which can be used if the request does not belong to a non-anonymous proxy.

However, if the request comes from an anonymous proxy server, then it is not possible to directly obtain the IP address of the client. This is why they are called anonymous proxies.

+3
Dec 15 '09 at 13:10
source share

In your aspx file, make sure you have valid page decals at the top. For some reason mine was commented out.

Somehow this happened:

 <%--<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cookieTracker.aspx.cs" Inherits="comp466TMA1.cookieTracker" %>--%> 

Instead:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cookieTracker.aspx.cs" Inherits="comp466TMA1.cookieTracker" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Cookie Tracker</title> </head> <body> Hey, this is a site tracker using a cookie. You have visited this site:<%= visits %> times. Your ip address is <%= incomingIp %> </body> </html> 
0
Dec 20 '18 at 0:37
source share
 string result = string.Empty; string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (!string.IsNullOrEmpty(ip)) { string[] ipRange = ip.Split(','); int le = ipRange.Length - 1; result = ipRange[0]; } else { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } 
-one
Dec 02 '16 at 12:07 on
source share



All Articles