Get IP or web visitor's location

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
I have the following codes to locate our website visitor's location. and I think the code below is only reading to our server's location.
It works but I do not think this is something we can get web visitor's current location.

My question is:
1. Above statement. do you think it is right?
2. Do you know any ways I can find our website visitor's current location regardless it is on mobile or PC?  I understood there is some proxy or some router may not indicate the current location. That is something I can live with. But I do need some ways to get their location instead of just our server address.



  public static Domain.IpInfo GetCurrentPosition()
        {
            //https://ipinfo.io/developers/replacing-getcurrentposition
            Domain.IpInfo ipInfo = new Domain.IpInfo();
            try
            {
                string info = new WebClient().DownloadString("http://ipinfo.io/geo");
                ipInfo = JsonConvert.DeserializeObject<Domain.IpInfo>(info);

                RegionInfo myRI1 = new RegionInfo(ipInfo.Country);
                ipInfo.Country = myRI1.EnglishName;

                RegionInfo myCity = new RegionInfo(ipInfo.City);
                ipInfo.City = myCity.EnglishName;

                RegionInfo myState = new RegionInfo(ipInfo.Region);
                ipInfo.Region = myState.EnglishName;

                RegionInfo myZip = new RegionInfo(ipInfo.Postal);
                ipInfo.Postal = myZip.EnglishName;

                RegionInfo myLoc = new RegionInfo(ipInfo.Loc);
                ipInfo.Loc = myLoc.EnglishName;            
            }
            catch (Exception)
            {
               // ipInfo = null;
            }

            return ipInfo;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
1. Your code is indeed for your server's location only.
2. Slightly adjust your code to do the lookup for HttpContext.Current.Request.UserHostAddress
Then pass it on to the website using the steps detailed here: https://ipinfo.io/developers
Obviously, no one gets work done for free, so you should check if you're will to pay for it: https://ipinfo.io/pricing (cheapest is 250K lookups for some 41 bucks per month)
As you noted, in some cases, you'll get the IP nr of a proxy/VPN service, and even if the visitor DIDN'T actively used anything to hide, some IP nr's just don't have a known location, and a rough estimate is used (sometimes even 50km off)

Author

Commented:
change to HttpContext.Current.Request.UserHostAddress won't help. it returns ::1
I have been using ipinfo already.

Commented:
Please test the code, with the browser on your phone or other laptop/workstation. If you test it on the server, with the browser of the same server, that's what you will get as a result.

Author

Commented:
I test PC in three different location. the end result is the same

Commented:
Please show the full page/code

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial