Get public site user IP

Asked

Viewed 119 times

1

Good evening, everyone

I made a web application with ASPNET and C#, and I need to get the user’s IP when he accesses the system.

I researched if I found several ways, but I always have this return::1

Follows below the code used:

Response.Write(Request.ServerVariables["REMOTE_Host"].ToString());

I searched here on Stackoverflow and found the code below, but returns me the ip from where the request is hosted:

 public static string GetPublicIP()
    {
        string url = "http://checkip.dyndns.org";
        System.Net.WebRequest req = System.Net.WebRequest.Create(url);
        System.Net.WebResponse resp = req.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
        string response = sr.ReadToEnd().Trim();
        string[] a = response.Split(':');
        string a2 = a[1].Substring(1);
        string[] a3 = a2.Split('<');
        string a4 = a3[0];
        return a4;
    }

I don’t know what else to do.

So, guys. How do I get the public ip of the user who accessed my system?

Vlw..

  • What version of . net? Core or "full framework"?

  • .NET 4.6.1, Webform

  • This return not because you are running the application locally on your machine?

  • Do you want whose IP? The user who is accessing the application or the machine that is running the application (your server)?

  • I want the IP address of who is accessing the application.

1 answer

0

To get the public IP of your application visitors, you can use the server variables, which is what you’re doing but not in the optimal way.

The variable REMOTE_HOST, which is the one you are reading, returns the name of the customer’s host. Only if the server does not have this information will it return the value of the variable REMOTE_ADDR, and this yes contains the host IP.

However, some users may be accessing your site through a proxy. In this case, the REMOTE_HOST variable will return the IP of the proxy server and not the client, which is what we want. Some proxy servers send the client’s final IP in the request header, so it’s good practice to always check it first. The IP sent, in this case, is in the variable HTTP_X_FORWARDED_FOR.

The problem with this variable is that it can have more than one comma-separated IP address. The logic then becomes:

  1. Check if the HTTP_X_FORWARDED_FOR variable exists
  2. If it exists, we must try to break it by comma and return the first address
  3. If it does not exist, return the value of the REMOTE_ADDR variable

The code goes something like this:

private string GetIP()
{
    var ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrWhiteSpace(ip))
    {
        var ips = ip.Split(',');

        return ips.First();
    }

    return Request.ServerVariables["REMOTE_ADDR"];
}

When you run locally, the IP will be ::1 because this is the Ipv6 version of 127.0.0.1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.