Click here to Skip to main content
16,016,425 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an ASP.NET website written in C#.

On this site I need to automatically show a start page based on the user's location.

Can I get name of user's city based on the IP address of the user ?
Posted
Updated 5-Jan-17 15:51pm

You need reverse geo-coding API based on IP address. Check these:
Get user location by IP address[^]
Getting ipaddress and location of every user visiting your website[^]
 
Share this answer
 
Comments
bjdestiny 25-Nov-13 1:35am    
I liked this... Bookmarked it & +5..
ridoy 25-Nov-13 12:55pm    
thank you,:)
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Text;
using System.IO;
using System.Xml;
using System.Net;
using System.Net.Sockets;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //this line is to check the clien ip address from the server itself
        string IP = "";

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

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

        IPAddress[] addr = ipEntry.AddressList;

        IP = addr[2].ToString();

        //Initializing a new xml document object to begin reading the xml file returned
        XmlDocument doc = new XmlDocument();           
        doc.Load("http://www.freegeoip.net/xml");       
        XmlNodeList nodeLstCity = doc.GetElementsByTagName("City");     
        IP = ""+ nodeLstCity[0].InnerText + "<br>" + IP;
        Response.Write(IP);
        //this is my header that I love
        
    }
}

[Edit:] Code block added
 
Share this answer
 
v3
Comments
Codes DeCodes 26-Mar-14 7:08am    
i also needed this in my web application..i tried it.. as this code reaches http://www.freegeoip.net/xml, it shows me error "Unable to connect to the remote server".. i typed the address in my browser http://www.freegeoip.net was opened and address of my city was opened.. now as i typed http://www.freegeoip.net/xml/(myIP) in browser , "RD Reserved" is shown... any help in what is happening??
veerendrasingh 19-Apr-16 8:43am    
is http://www.freegeoip.net website is trustable?
http://freegeoip.net/json/[^]

Request to this url and parse json to get user's country,city,IP ....
 
Share this answer
 
Even a simpler solution then you have implemented
C#
string ip = Server.HtmlEncode(Request.UserHostAddress);

XmlDocument doc = new XmlDocument();

string getdetails= "http://www.freegeoip.net/xml/" + ip;

doc.Load(getdetails);

XmlNodeList nodeLstCity = doc.GetElementsByTagName("City");

string location = nodeLstCity[0].InnerText;
 
Share this answer
 
Comments
veerendrasingh 19-Apr-16 8:43am    
http://www.freegeoip.net is not working...
Thanks7872 20-Apr-16 2:28am    
It was working in 2013 :-)
http://stackoverflow.com/questions/4327629/get-user-location-by-ip-address
 
Share this answer
 
I think you had not searched for your problem in CP.
Anyway just visit This[^], This1[^] and This[^] links you will get your solution.
 
Share this answer
 
Comments
Maddy selva 28-Nov-13 1:01am    
I have solved this my self!..
At timezoneapi.io you can request an IP address using cURL. It returns a few objects including location, timezone and datetime.

$curl https://timezoneapi.io/developers/ip-address


Returns
- Location including latitude / longitude
- timezone (time zone variables e.g. capital, currency, phone prefix, shortcodes etc.
- datetime (the date / time for the timezone when the request was made + lots of date / time variables).

Take a look here for API documentation:
IP address. Get users location, time zone and date/time details. - Timezone API[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900