Hi All,
Im working on a project for work, and part of this project is getting the IP from a host and translating it into Lattitude and Longitude(code for this is below). The problem I am facing currently is that when I try to get the IP, I get "::1" as my IP, which then gives me the Lat and Lon coordinates for some place in Hong Kong. What Can I Do to fix this? this is my first time programming something like this.
Thanks in Advance!

P.S to get the Lat and Lon I use GoogleMaps.LocationServices from NuGet.
Link: http://www.nuget.org/packages/GoogleMaps.LocationServices

Full Code:

using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Script.Serialization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using GoogleMaps.LocationServices;

namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(ipAddress))
            {
                ipAddress = Request.ServerVariables["REMOTE_ADDR"];
            }

            var locationService = new GoogleLocationService();
            var point = locationService.GetLatLongFromAddress(ipAddress);

            var latitude = point.Latitude;
            var longitude = point.Longitude;
            Lat.Text = latitude.ToString();
            Lon.Text = longitude.ToString();
            IP.Text = ipAddress;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString);
           SqlCommand cmd = new SqlCommand("INSERT INTO Observations(UserID, DateTime, Loc_Lat, Loc_Lon, Observation, Comments) VALUES (@UserID, @DateTime, @Loc_Lat, @Loc_Lon, @Observation, @Comments)", conn);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.AddWithValue("@UserID", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Observation", TextBox2.Text);
            cmd.Parameters.AddWithValue("@DateTime", DateTime.Now);
            cmd.Parameters.AddWithValue("@Comments", TextBox3.Text);
            cmd.Parameters.AddWithValue("@Loc_Lat", Lat);
            cmd.Parameters.AddWithValue("@Loc_Lon", Lon);
            conn.Open();
            cmd.ExecuteNonQuery();
            Response.Redirect("Webform1.aspx");
        }
    }


}

Section That concerns Lat/Lon and IP:

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(ipAddress))
            {
                ipAddress = Request.ServerVariables["REMOTE_ADDR"];
            }

            var locationService = new GoogleLocationService();
            var point = locationService.GetLatLongFromAddress(ipAddress);

            var latitude = point.Latitude;
            var longitude = point.Longitude;
            Lat.Text = latitude.ToString();
            Lon.Text = longitude.ToString();
            IP.Text = ipAddress;

        }

545b801706005195393b2dd810088499

Recommended Answers

All 8 Replies

All you're seeing is your IPv6 local host address. Effectively 127.0.0.1 in IPv4

You will need a service that exists outside your local area network that can tell you your IP Address. Google will be able to give you a list of those (and might even have something to tell you your IP address)

Looks like you are running this from Visual Studio (website and browser)? Once you host this web on a server and browse to it from a remote system, you'll be able to pull in the source computer's IP address.

As Ketsuekiame indicates the ::1 you are seeing is because you are on a system with IPv6 enabled.

would something like this work?

            string strHostName = "";
            strHostName = System.Net.Dns.GetHostName();

            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

            IPAddress[] addr = ipEntry.AddressList;
            string ipAddress = addr[addr.Length - 2].ToString();

I can see you using DNS.GetHostName for an intranet application but if this will be used on the Internet, I do t think it would be much help. I'd use:

 Request.ServerVariables["REMOTE_ADDR"];

He's already using that. Line 22 from the first section and Line 8 on the second.

If this is going to be a public (internet) web page, you shouldbe okay actually. But if you're going to use it as an intranet page you will need to hit an external service to find the IP. Not sure there's going to be an easy way to do that.

But if you're going to use it as an intranet page you will ...

As long as the browser and the web server are on two different systems, Request.ServerVariables["REMOTE_ADDR"] will provide the IP where the traffic is coming from.

An IP is an IP regardless if its an intranet or internet address.

If you are on an intranet and need more information other than the IP, say DNS Hostname, it would make more sense to leverage System.Net.Dns.

As long as the browser and the web server are on two different systems, Request.ServerVariables["REMOTE_ADDR"] will provide the IP where the traffic is coming from.

It matters for his application. He's wanting to get the GPS location of the IP Address. This wouldn't make sense for an internal IP Address.
Any of the following for that matter;
10.0.0.0/8
172.16.0.0/12
192.168.0.0/16

Any network can run these internally, in any combination (depending on routing). They're private addresses. If a user is connecting from the intranet, to an internal site, you're only going to get a private address. You may not even be able to use System.Net.Dns depending on how the network is configured as, again, an internal address has no meaning in an external context. No public DNS is going to know about your internal network. If the internal DNS is set up correctly, you should be able to query the hostname to get the external facing address, but I typically find there is no reverse lookup to make this easy.

If it's an external site, then yes, that's fine, which I agreed with before :)

Yes, I'm very familiar with TCP/IP. My last few posts were simply to point out the differences between using either approach in an intranet vs an external network such as the Internet.

I should have taken into account the objective, which is the Geolocation component.... Thanks for the clarification.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.