3

I have already tried all solutions in Stack Overflow question How to get a user's client IP address in ASP.NET?. It gives me the server's IP address, not the client's LAN IP address.

I need to get client's local IP address, and it must be seen as 192.168.2.1, but I always get the server's IP address instead of it.

Here is my code:

System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["LOCAL_ADDR"];

There is a website doing this, http://net.ipcalf.com/.

How do I solve this issue?

Community
  • 1
  • 1
FreeCLoud
  • 79
  • 8
  • no idea, but you can look at their soruce code for the answer, well assuming it's not a server side thing (which I seriously doubt) – LiranBo Oct 26 '15 at 16:32
  • 5
    Possible duplicate of [How to get a user's client IP address in ASP.NET?](http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net) – Marko Oct 26 '15 at 16:36
  • they're using an `RTCPeerConnection`. You can literally copy the HTML and script on that page into a text document and get the same results in Chrome and Firefox. They are doing it clientside – Jonesopolis Oct 26 '15 at 16:39
  • You can't get the local client address any other place than at the client. It's not sent along in the request. – Guffa Oct 26 '15 at 16:39
  • As far as I know, the only thing that worked for me was using ActiveX with Internet Explorer, but that makes the web site very restrictive ;) If that is no problem check: http://codingresource.blogspot.nl/2010/02/get-client-mac-address-ip-address-using.html?m=1 – Silvermind Oct 26 '15 at 16:44

2 Answers2

1

You want the remote address, not the local address

context.Request.ServerVariables["REMOTE_ADDR"]

Todd Sprang
  • 2,899
  • 2
  • 23
  • 40
-2

Your code grabs the server IP address, not the client's. Try the following below:

This code was copied from Stack Overflow question How to get a user's client IP address in ASP.NET?.

protected string GetIPAddress()
{
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (!string.IsNullOrEmpty(ipAddress))
    {
        string[] addresses = ipAddress.Split(',');
        if (addresses.Length != 0)
        {
            return addresses[0];
        }
    }

    return context.Request.ServerVariables["REMOTE_ADDR"];
}
Community
  • 1
  • 1
Marko
  • 2,734
  • 24
  • 24
  • 4
    Then you should have marked this question as a duplicate – wingerse Oct 26 '15 at 16:34
  • I have flagged it but in the meantime i also provided an answer. – Marko Oct 26 '15 at 16:37
  • 1
    this topic is not duplicate because the quuestion you mentioned is about get users wan ip adress like 86.123.127.8 but what am i asking is how can you get user's local ip adress like 192.168.2.1 i wish you read a litte more about the question before judging. – FreeCLoud Feb 16 '16 at 13:17