How to get IP address of user.

Recommended Answers

All 4 Replies

HttpContext.Current.Request.UserHostAddress

This doesn't attempt to take into account proxies. For that, you can use Request.ServerVariables["HTTP_X_FORWARDED_FOR"]. However, make sure you're not trusting that blindly, since it could be forged. It's better to keep a whitelist of IPs for which you trust it.

Other example:

String strHostName = new String("");
                        // Getting Ip address of local machine...
                        // First get the host name of local machine.
                        strHostName = DNS.GetHostName();

                        // Then using host name, get the IP address list..
                        IPHostEntry ipEntry = DNS.GetHostByName(strHostName);
                        IPAddress[] addr = ipEntry.AddressList;

or:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class how_to_Get_client_IP_address : System.Web.UI.Page
{

	protected void Page_Load(object sender, System.EventArgs e)
	{
		string ClientIP = null;
		ClientIP = Request.UserHostAddress;

		Label1.Text = ClientIP;
	}
	public how_to_Get_client_IP_address()
	{
		Load += Page_Load;
	}
}

But my question is how can i locate a city with respect to the Ip address , as in US so many cities will have same latitude and longitude values (or with slight variation in lat and long values)?

ASP.net web development you can get UserIPAddress using System.Web.HttpContext.Current.Request.UserHostAddress

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.