Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

Get IP or web visitor's location

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

ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ITsolutionWizard

ASKER

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

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.
I test PC in three different location. the end result is the same
Please show the full page/code