1

Dear All, how can I get the private ip of client system from a website hosted on public ip in ASP.Net C#? while i am checking userhostname, it's showing the global IP of the Internet connectivity which is the same for all the machines in the network. Please advice me.

Thanks in Advance

Anoop George Thomas

2 Answers2

4

Short answer is no, not from the server side. You may be able to write client-side code that looks up the system's actual IP address, though you may need to do it by way of a browser plugin.

A better question is "Why do you need do to that?" Generally, if you have to have the internal IP, you are doing something wrong.

Justin Morgan
  • 2,427
  • 2
  • 16
  • 19
  • Justin, I m using the code to restrict an online voting to be done only once from a particular machine.Is there any alternative? – Anoop George Thomas Feb 02 '11 at 05:18
  • 1
    There's no silver bullet, since the client can easily spoof HTTP_X_FORWARDED_FOR. The "right" solution is to require user accounts and only allow each account to vote once. Otherwise you can use a combination of cookies, IP address, and the user agent (in its entirety) to thwart amateur attempts. – Justin Morgan Feb 02 '11 at 05:21
2

Unfortunately, it's not something you'll be able to pull.

Now, you can grab an IP of a machine that's coming through a proxy or other some such forwarding device. Here's an example function that utilizes the HTTP_CLIENT_IP and the HTTP_X_FORWARDED_FOR IP if they are available in your server environment.

<?php

function getIP(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip = $_SERVER['HTTP_CLIENT_IP'];
    }else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
      $ip = $_SERVER['REMOTE_ADDR'];
    }

    return $ip;
}


echo getIP();

?>
jerebear
  • 6,503
  • 4
  • 31
  • 38