0

The following code works OK locally, but it will only get the server's IP (if I'm correct).

try
{
    string externalIP;
    externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
    externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                 .Matches(externalIP)[0].ToString();
    model.IpCreacion = externalIP;
}
catch { }

I can't test this right now, because the two guys at my office that can make this as a public URL for testing on a server aren't here today. The code is in the controller of the project, so it runs on the server every time a client executes the app, it's not actually the client who is getting the IP address.

How can I make the client get his IP address, instead of the server, executing the code I just showed?

If I managed to put this functionality in a view, would it work as I'm intending to?

UPDATE: I tried other methods posted as answers, like

string ip = System.Web.HttpContext.Current.Request.UserHostAddress;

and

model.IpCreacion = null;
model.IpCreacion = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (string.IsNullOrEmpty(model.IpCreacion))
{
    model.IpCreacion = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}

but now I'm only getting ::1 as a result. Which didn't happen before, as I was getting a correct IP address.

  • The IP address will be whatever is the IP address of the gateway that is public facing, which might NOT be the IP address of the server (firewalls, load balancing, proxy etc can all get in the way of the server). – Neil Mar 09 '20 at 14:03
  • https://stackoverflow.com/questions/391979/how-to-get-clients-ip-address-using-javascript This answer has a ton of methods for obtaining IP's of the client. – feyd Mar 09 '20 at 14:04
  • Does this answer your question? [How to Get IP Address?](https://stackoverflow.com/questions/1907195/how-to-get-ip-address) – Neil Mar 09 '20 at 14:04
  • Does this answer your question? [How can I get the client's IP address in ASP.NET MVC?](https://stackoverflow.com/questions/2577496/how-can-i-get-the-clients-ip-address-in-asp-net-mvc) – Ryan Thomas Mar 09 '20 at 14:14
  • Not really @Neil, I mean, I get `::1` check the update in the question, please – fzappaandthem Mar 09 '20 at 14:32
  • Not working fine @RyanThomas please check the update on the question, I'm getting `::1` now – fzappaandthem Mar 09 '20 at 14:33
  • 1
    ::1 (which is localhost/127.0.0.1) means your server is behind a firewall/loadbalancer/proxy and isn't configured for forwarding on the header correctly. – Neil Mar 09 '20 at 14:38
  • Well, @Neil everyone from work will have that problem, and checking "http://checkip.dyndns.org/", worked OK, but as I stated, I think the problem is that the server is going to always return its IP address' and not the client's. I think I've seen in one of the links something to do with this, I'll give it a shot. – fzappaandthem Mar 09 '20 at 14:43
  • You are correct, everyone DOES have this problem, which is why I said you need to configure whatever server is between the internet and the server running your application. The header `HTTP_X_FORWARDED_FOR` is added by your firewall/proxy/load balancer, once it has been configured correctly for exactly the purpose you require. – Neil Mar 09 '20 at 14:46
  • Thanks @Neil, I will have to talk to the same guys I was talking in my original question for that, since they are who manage that area. – fzappaandthem Mar 09 '20 at 14:54

3 Answers3

0

That gets only IP of server, because you send the request from Web Server to checkip.dyndns.org.

To get the client IP, you need to use JavaScript and do the same thing.

$.get('http://checkip.dyndns.org/', function(data) {
    console.log(data); // client IP here.
})

UPDATED:

If you need client IP Address in ASP.NET Core, you can inject this service

private IHttpContextAccessor _accessor;

And use it as

_accessor.HttpContext.Connection.RemoteIpAddress.ToString()

Or in ASP.NET Framework

Public string GetIp()  
{  
    string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
    if (string.IsNullOrEmpty(ip))  
    {  
       ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
    }  
    return ip;  
} 
Alex - Tin Le
  • 1,982
  • 1
  • 6
  • 11
0

if you want to get client ip address,visit bellow post in stackoverflow How can I get the client's IP address in ASP.NET MVC?

Vahid ST
  • 27
  • 4
0
Public string GetIp()  
{  
    string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
    if (string.IsNullOrEmpty(ip))  
    {  
       ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
    }  
    return ip;  
}  
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28