C# IP Address always ::1 - c#

I've tried something explained below, somehow, IP Address is always ::1, on both local and server side,
Here are the codes, I tried;
m_CallerIP = string.IsNullOrEmpty(req.ServerVariables["HTTP_CLIENT_IP"]) ? req.UserHostAddress : req.ServerVariables["HTTP_CLIENT_IP"];
string Port = string.IsNullOrEmpty(req.ServerVariables["HTTP_CLIENTPORT"]) ? req["HTTP_CLIENTPORT"] : req.ServerVariables["CLIENTPORT"];
string asd2 = String.IsNullOrEmpty(req.ServerVariables["REMOTE_ADDR"]) ? req.UserHostAddress : req.ServerVariables["REMOTE_ADDR"];
string asd23 = String.IsNullOrEmpty(req.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? req.UserHostAddress : req.ServerVariables["HTTP_X_FORWARDED_FOR"];
string asd4 = req.UserHostAddress;
IPAddress address = IPAddress.Parse(m_CallerIP);
IPAddress address1 = IPAddress.Parse(asd2);
IPAddress address2 = IPAddress.Parse(asd23);
IPAddress address3 = IPAddress.Parse(asd4);
Is there anyone to know why and how to solve it ?
Thanks in Advance!

The code bellow works for me.
When I run the app in localhost I always get ::1. But, it works greate on server side. The app is running in Azure.
string ip = "";
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ip))
ip = ip.Split(',').Last().Trim();
else
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (ip == "::1")
ip = "";
if (ip.IndexOf(':') > 0)
ip = ip.Substring(0, ip.IndexOf(':'));
return ip;

Related

How to Find User's IP address

I need help to find user's IP address who are browsing to my site (using C# Code).
I am confused about system IP address, ISP IP address, Network address and what is IPV4 and IPV6.
Below is the code I got from other source:
protected string GetUserIP()
{
string userIP = string.Empty;
HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
userIP = addresses[0];
}
}
else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
{
userIP = HttpContext.Current.Request.UserHostAddress;
}
return userIP;
}
But here I am not getting the expected result.
When test this in my local-host I got 127.0.0.1
When I deploy this into my development server and browse the page in my local machine, I got different address.
I tried accessing the same URL in other machine and IP address are same.
The systems I am using to access my development server deployed page in in same network.
So is it the network IP my code returned..?
Because when I find my IP using ipconfig command it's again a different!
And when I checked my IP using 3rd party site (http://www.whatismyip.com/ip-address-lookup/), it shows the actual IP I expected. How can I get that actual IP using C# code?
My ultimate goal is, get the user location details who is browsing my site using Maxmind City DB, but for that I need to pass the IP of browsing user.
string ip = GetUserIP();
string db = “GeoIP2-City.mmdb";
var reader = new DatabaseReader(db);
var city = reader.City(ip);
double? lat = city.Location.Latitude;
double? lang = city.Location.Longitude;
For the IP I got from my above code, I got exception (IP not found in Database) when I tried to get information from Maxmind DB,
but for the IP I got from "whatismyip" website, I got the details from the Maxmind DB.
Hope My question is clear.
Please let me know if anyone have any inputs.
Thanks,
Sharath
I guess this will help
public String GetLanIPAddress()
{
//The X-Forwarded-For (XFF) HTTP header field is a de facto standard for identifying the originating IP address of a
//client connecting to a web server through an HTTP proxy or load balancer
String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return ip;
}

Wrong client IP address

I'm getting client IP address from ASP.NET. But some client IP address received 127.0.0.1.
What is problem. How getting valid client ip address?
I'm using this code:
public static string GetIP()
{
string clientIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(clientIp))
{
string[] forwardedIps = clientIp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
clientIp = forwardedIps[forwardedIps.Length - 1];
}
if (string.IsNullOrEmpty(clientIp))
clientIp = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"];
if (string.IsNullOrEmpty(clientIp))
clientIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
return clientIp.ToString();
}
127.0.0.1 is localhost, i.e. the same machine is making the request as is hosting it.
My guess is that what you are seeing is infact your own testing or debugging?
I'd consider Request.IsLocal() as a good way to find out.

How to get LAN IP of a client using .net?

I want to get the adrress IP (LAN ip) of the computer which access my site.
How can i get it?
You can't.
Browsers don't send their local IPs in HTTP headers, so there is no way for you to get it. You only get the router's external (internet) IP.
Method 1:
You can get that by using below mentioned link.
List the IP Address of all computers connected to a single LAN
Method 2:
You can try below one also.
public string GetUserIP()
{
string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipList))
{
return ipList.Split(',')[0];
}
return Request.ServerVariables["REMOTE_ADDR"];
}
Method 3:
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
I hope this will help to you.
I assume you're using asp.net and in that case you could use Request.UserHostAddress to retrieve the IP address of a client.
Use this function:
public string GetLanIP()
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}

get device ip address from service provider ip address

I need get the local machine ip address of my website visited users for that i used below code
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
stradd = addresses[0];
}
else
{
stradd = ipAddress;
}
}
else
{
stradd = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
hostName = Dns.GetHostByAddress(stradd).HostName;
this is giving the ip address of the service provider & name of the service provider but i don't want this i wanted user device(local) ip address, is it possible to get local ip address? please help me.
No. You can't get the private IP address of a machine hidden behind NAT.
The router relays the request transparently.
The local IP is just that (local) it will never leave the local router, you will need to use some sort of locally executed code to send it to the server.

Get A Computer's Dynamic Public IP Address

I've managed to get my static IP Address and some other mac addresses.
Using this code :
IPAddress[] addr = Dns.GetHostEntry( Dns.GetHostName() ).AddressList;
string dynamicip = addr[addr.Length - 3].ToString();
Any idea how to get the dynamic public address like the one on the site whatismyip.com?
whatismyip.com has an api (sort of) setup for this purpose here. you can use
public static IPAddress GetExternalIP()
{
string url = "http://www.whatismyip.com/automation/n09230945.asp";
WebClient webClient = new WebClient();
string response = utf8.GetString(webClient .DownloadData(whatIsMyIp));
IPAddress ip = IPAddress.Parse(response);
return ip;
}
I think this is probably the question that you're asking: How to get the IP address of the server on which my C# application is running on?
for IP Address you can use HttpContext.Current.Request.UserHostAddress;
for windows form check How to detect static ip using win app in c#
brgds

Categories

Resources