Get The Public IP Address Of The User Who Is Visiting My Website
Heading ##Hey All Thanks For Your Contribution ,I Got Answer By Using The Following Code,Hope It Might Help Some One In Future ## Heading ##
public string GetVisitorIPAddress(bool GetLan = false)
{
string visitorIPAddress = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (String.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(visitorIPAddress))
visitorIPAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1")
{
GetLan = true;
visitorIPAddress = string.Empty;
}
if (GetLan)
{
if (string.IsNullOrEmpty(visitorIPAddress))
{
//This is for Local(LAN) Connected ID Address
string stringHostName = Dns.GetHostName();
//Get Ip Host Entry
IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
//Get Ip Address From The Ip Host Entry Address List
IPAddress[] arrIpAddress = ipHostEntries.AddressList;
try
{
visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString();
}
catch
{
try
{
visitorIPAddress = arrIpAddress[0].ToString();
}
catch
{
try
{
arrIpAddress = Dns.GetHostAddresses(stringHostName);
visitorIPAddress = arrIpAddress[0].ToString();
}
catch
{
visitorIPAddress = "127.0.0.1";
}
}
}
}
}
var zaz = "";
zaz = visitorIPAddress.ToString();
getcityname(zaz);
return null;
}
Get the public IP address of a client
HttpContext.Current.Request.UserHostAddress
This should get the public IP address of a client
EDIT
So you're after the IP address of your own machine as the rest of the world sees it??
This only really matters if you're hosting in on lets say an Intranet where the clients will be on the same network and thats the only address you will get if you use HttpContext.Current.Request.UserHostAddress
. You will need to use a lookup api or something in this case. But then you'd know the external IP of your network!
If this is to be hosted on the internet then HttpContext.Current.Request.UserHostAddress
will work just fine as every client will be displaying it's external IP.
It's showing 127.0.0.1 I'm guessing because you're testing on your local machine and this will be it's loopback address.
Hope this helps
How to get a user's client IP address in ASP.NET?
As others have said you can't do what you are asking. If you describe the problem you are trying to solve maybe someone can help?
E.g.
- are you trying to uniquely identify your users?
- Could you use a cookie, or the session ID perhaps instead of the IP address?
Edit The address you see on the server shouldn't be the ISP's address, as you say that would be a huge range. The address for a home user on broadband will be the address at their router, so every device inside the house will appear on the outside to be the same, but the router uses NAT to ensure that traffic is routed to each device correctly. For users accessing from an office environment the address may well be the same for all users. Sites that use IP address for ID run the risk of getting it very wrong - the examples you give are good ones and they often fail. For example my office is in the UK, the breakout point (where I "appear" to be on the internet) is in another country where our main IT facility is, so from my office my IP address appears to be not in the UK. For this reason I can't access UK only web content, such as the BBC iPlayer). At any given time there would be hundreds, or even thousands, of people at my company who appear to be accessing the web from the same IP address.
When you are writing server code you can never be sure what the IP address you see is referring to. Some users like it this way. Some people deliberately use a proxy or VPN to further confound you.
When you say your machine address is different to the IP address shown on StackOverflow, how are you finding out your machine address? If you are just looking locally using ipconfig
or something like that I would expect it to be different for the reasons I outlined above. If you want to double check what the outside world thinks have a look at whatismyipaddress.com/.
This Wikipedia link on NAT will provide you some background on this.
Get local IP address
To get local Ip Address:
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
To check if you're connected or not:
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
Related Topics
Testing If Object Is of Generic Type in C#
Converting a Base 64 String to an Image and Saving It
Interprocess Communication for Windows in C# (.Net 2.0)
How to Validate Console Input as Integers
Richtextbox Syntax Highlighting in Real Time--Disabling the Repaint
How Do the Major C# Di/Ioc Frameworks Compare
How to Edit the Visual Studio Templates for New C# Class/Interface
How to Pass Parameters to Threadstart Method in Thread
Use Multiple Jwt Bearer Authentication
How to Convert an Ipv4 Address into a Integer in C#
Typeloadexception Says 'No Implementation', But It Is Implemented
What Is the Use of Observablecollection in .Net
How to Ignore Unknown Enum Values During JSON Deserialization
What Static Analysis Tools Are Available for C#