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;
}
Related
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
I have q requirement of getting the IP Address of clients who have accessed URL based on domain name. For example I am accessing www.google.com from my machine(IP Address : 172.16.58.989) I should log IP Address as 172.16.58.989 on google server. What I am doing In the code is
protected string GetIPAddress()
{
String ip = HttpContext.Current.Request.UserHostAddress;
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
if (string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return ip;
}
But when I access the site using domain name (www.google.com) I am getting the server address. when I am accessing the site with ip address like http://11.11.11.11/aspx/login.aspx I am getting client ip.
Please give me your suggestions.
I have already tried all solutions in Stack Overflow question How to get a user's client IP address in ASP.NET?. It gives me the server's IP address, not the client's LAN IP address.
I need to get client's local IP address, and it must be seen as 192.168.2.1, but I always get the server's IP address instead of it.
Here is my code:
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["LOCAL_ADDR"];
There is a website doing this, http://net.ipcalf.com/.
How do I solve this issue?
You want the remote address, not the local address
context.Request.ServerVariables["REMOTE_ADDR"]
Your code grabs the server IP address, not the client's. Try the following below:
This code was copied from Stack Overflow question How to get a user's client IP address in ASP.NET?.
protected string GetIPAddress()
{
System.Web.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)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
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/
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.