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

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!");
}

Related

How to get local device IP in ASP.NET Core MVC

I'm working on a ASP.NET Core project where I have a Login form, and I want to identify device from where the user is trying to login. This is what I find and what I use to get the IP:
userLogin.stringIP=Request.HttpContext.Connection.RemoteIpAddress.ToString();
With this I get only Local Gateway IP instead of local IP of device.
Update
The example code works,but when I publish the Website on my IIS Server it gives me the IP of IIS server, instead of device from where I try to acces the Website For example when I open the Website that is published on IIS server it gaves me the IP of IIS server.I need the device IP address from which I try to access the WebSite.
Any ideas / suggestions how to get local IP of device.
Thanks in advance!
Try any one from the below:
var ip1 = Request.HttpContext.Connection.LocalIpAddress.ToString();
var ip2 = Request.HttpContext.Connection.RemoteIpAddress.ToString();
var ip3 = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress.ToString();
var ip4 = HttpContext.Features.Get<IHttpConnectionFeature>()?.LocalIpAddress.ToString();
string remoteIpAddress = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
if (Request.Headers.ContainsKey("X-Forwarded-For"))
remoteIpAddress = Request.Headers["X-Forwarded-For"];
The below code will return the local IP Address:
publicl static string GetLocalIpAddress()
{
UnicastIPAddressInformation mostSuitableIp = null;
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var network in networkInterfaces)
{
if (network.OperationalStatus != OperationalStatus.Up)
continue;
var properties = network.GetIPProperties();
if (properties.GatewayAddresses.Count == 0)
continue;
foreach (var address in properties.UnicastAddresses)
{
if (address.Address.AddressFamily != AddressFamily.InterNetwork)
continue;
if (IPAddress.IsLoopback(address.Address))
continue;
if (!address.IsDnsEligible)
{
if (mostSuitableIp == null)
mostSuitableIp = address;
continue;
}
// The best IP is the IP got from DHCP server
if (address.PrefixOrigin != PrefixOrigin.Dhcp)
{
if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
mostSuitableIp = address;
continue;
}
return address.Address.ToString();
}
}
return mostSuitableIp != null
? mostSuitableIp.Address.ToString()
: "";
}
If the asp.net core project is located outside the firewall of the local device from which the user is logging in and the user is using a private IP address (RFC 1918) then his/her local firewall will strip out the private IP address and replace that with the public facing IP address as source IP address.
Have you tried the solution provided by mrchief in Get local IP address?
He (she?) uses Dns.GetHostEntry(Dns.GetHostName()) to get the local IP address of the machine.
Worth trying.
Regards
N

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/

Get Web Api consumer IP Address and HostName in ASP.NET Web API

I have hosted Web Api developed in ASP.net. I want if someone call my API so I can log in database so later on if I want to reject request from particular id using C#.
What is best practice to get consumer IP and HostName?
public static string GetIP4Address()
{
string IP4Address = String.Empty;
foreach (IPAddress IPA in Dns.GetHostAddresses(Request.ServerVariables["REMOTE_ADDR"].ToString())))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
}
if (IP4Address != String.Empty)
{
return IP4Address;
}
foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (IPA.AddressFamily.ToString() == "InterNetwork")
{
IP4Address = IPA.ToString();
break;
}
}
return IP4Address;
}
Resolving names sounds like a bad idea. What if you are called from a client machine that dns doesn't resolve? I tell you, dns takes forever and then fails with an exception you have to catch and log as "unknown host name".
As for request address, just get it with
HttpContext.Current.Request.UserHostAddress

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

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