2

I Use this code to get the public IP-address (thanks to this post How to get the IP address of the server on which my C# application is running on?):

    public static string GetPublicIP()
    {
        try
        {
            String direction = "";
            WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader stream = new StreamReader(response.GetResponseStream()))
                {
                    direction = stream.ReadToEnd();
                }
            }

            //Search for the ip in the html
            int first = direction.IndexOf("Address: ") + 9;
            int last = direction.LastIndexOf("</body>");
            direction = direction.Substring(first, last - first);

            return direction;
        }
        catch (Exception ex)
        {
            return "127.0.0.1";
        }
    }

But no matter who that access my website, they all get the same IP, and it is the server public IP, not the current user's IP.

Is it possible to run the WebRequest in context of the current user, not as the server?

Or is the problem that I run this function inside App_Code so that the current user Request is not available, instead it use the server context?

Please help!

Community
  • 1
  • 1
Martin at Mennt
  • 5,677
  • 13
  • 61
  • 89
  • The web site call/request is happening from your server. That means in the HTTP packet sent by the server where this code is running, it will have your servers ipaddress in it. So if you want the clients ip address then why are you requesting some other website? Just open up the http packet from client. Hope my understanding of your problem is correct. :O – Zenwalker May 15 '12 at 09:25
  • Guess you are correct, but I dont get the correct IP if I use ex. Request.ServerVariables["REMOTE_ADDR"]. Then I get my local VPN IP not the public IP which I get if I use checkip.dyndns.org. – Martin at Mennt May 15 '12 at 09:33
  • What you want might not be doable if there is a VPN involved. Can you provide an example of what you expect to happen in a given situation ? – Alex May 15 '12 at 10:08
  • I was assuming you already had a web service running. You have to have WCF host your service. I guess I am not sure what you end-to-end story is. You can get started with WCF services with this [tutorial](http://www.c-sharpcorner.com/uploadfile/mikeliu88/implementing-a-basic-hello-world-wcf-service/) – Mohammed Ali May 15 '12 at 10:11
  • Are there any commercial libraries or something available for this? How does http://checkip.dyndns.org manage to give the correct address? – Martin at Mennt May 15 '12 at 11:56

3 Answers3

0

I am guessing this code is running on a web server - that you've got a page that let's clients check their IP address? If so, I suspect you've got confused. If not, please elaborate on where this is running.

If the code above is running on a server, then you will always get that server's public IP address if you make a call to a remote server and ask "what is the ip address this request came from" - which is what your code sample is doing.

If you want the IP address of the clients calling you - assuming you are a web application, then you should look at HttpWebRequest.UserHostAddress, altrhough be aware that this is not foolproof. Have a look here for more info.

Community
  • 1
  • 1
Rob Levine
  • 40,328
  • 13
  • 85
  • 111
  • Guess im a bit confused. Right now im connected to internet through a VPN, and my users might also be conencted through VPN. So if I try HttpWebRequest.UserHostAddress I then get my local VPN IP-address, and not my public address which I get if I visit http://checkip.dyndns.org. – Martin at Mennt May 15 '12 at 09:31
0

This is supposed to happen, the code is running on your machine so you get your own IP address. To get something from the user, you must check the headers sent by your users, specifically the REMOTE_ADDR header.

You can probably use Request.ServerVariables["REMOTE_ADDR"] in your code somewhere.

Davio
  • 4,609
  • 2
  • 31
  • 58
0

Since you are making the request from the server you will get the server's IP address.

I am not sure what sort of service you have. In the case of a WCF service, you can grab the IP address from the IncomingMessageProperties of the OperationContext object for the request. See an example here: Get the Client’s Address in WCF

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    string GetAddressAsString();
}

public class MyService : IMyService
{
    public string GetAddressAsString()
    {
        RemoteEndpointMessageProperty clientEndpoint =
            OperationContext.Current.IncomingMessageProperties[
            RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

        return String.Format(
            "{0}:{1}",
            clientEndpoint.Address, clientEndpoint.Port);
    }
}
Mohammed Ali
  • 1,027
  • 1
  • 9
  • 16
  • Thanks, but I get Object reference not set to an instance of an object on RemoteEndpointMessageProperty clientEndpoint = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; – Martin at Mennt May 15 '12 at 09:50
  • I implement it like this right? IMyService s = new MyService(); string ip = s.GetAddressAsString(); – Martin at Mennt May 15 '12 at 09:50
  • I was assuming you already had a web service running. You have to have WCF host your service. I guess I am not sure what you end-to-end story is. You can get started with WCF services with this [tutorial](http://www.c-sharpcorner.com/uploadfile/mikeliu88/implementing-a-basic-hello-world-wcf-service/) – Mohammed Ali May 15 '12 at 10:22
  • I have successfully implemented the WCF service now, but it return 127.0.0.1 as my IP. – Martin at Mennt May 15 '12 at 11:40
  • Very nice. Now, just to make sure, you are testing this from a machine that is different from where your service is hosted right? If not, then see if you can call your service from another machine to test this. – Mohammed Ali May 15 '12 at 17:05