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

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;
}

Related

How to get IP address of client from controller c# mvc

I have tried to get ip address of client but I just get ip of server, There is a way to get it? please, I want a function in the controller to get ip of client.
public static string GetIP()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}

How to obtain local ip address

I've managed to get clients' ip addresses from my server using codes below
OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ipaddress = endpoint.Address;
However, if clients are under the same network, all the ip addresses are public ip address of the network.
Is there a way to get local ip addresses of each client if they are under the same AP?
or just simply, is there a easier way to distinguish client from one another?
Because of network address translation it is not always possible to identify clients by unique client addresses see How to get a user's client IP address in ASP.NET?
try this
HttpRequest currentRequest = HttpContext.Current.Request;
string ipAddress = currentRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipAddress == null || ipAddress.ToLower() == "unknown")
ipAddress = currentRequest.ServerVariables["REMOTE_ADDR"];
return ipAddress;
You can try webrtc's in built feature to connect to STUN server https://diafygi.github.io/webrtc-ips/

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.

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

wcf client ip as ipv6

i'm using next piece of code to get client ip on wcf service :
OperationContext context = OperationContext.Current;
System.ServiceModel.Channels.MessageProperties prop = context.IncomingMessageProperties;
System.ServiceModel.Channels.RemoteEndpointMessageProperty endpoint = prop[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
string ip = endpoint.Address;
while this code worked on iis6/server2003 everything were ok, endpoint.Address returned ipv4.
but after i recently updated to iis7/server2008 endpoint.Address is returning ipv6.
is it still possible to get ipv4 on iis7/server2008 ?
Thank You !
This is not so much of a change in WCF that is a change in networking. Your client has used its IPv6 to connect to the server and that is the address which is stored in the context of the message. If you need to get hold of IPv4, use the snippet below:
IPAddress ipAddress = IPAddress.Parse(ipv6);
IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);
foreach (IPAddress address in ipHostEntry.AddressList)
{
if(address.AddressFamily == AddressFamily.InterNetwork)
Console.WriteLine(address);
}
This will translate your IPv6 to IPv4.

Categories

Resources