-
Oct 29th, 2021, 11:20 AM
#1
Just to be sure, get remote IP address
Hi.
I'm asking to be sure.
I do this to get a PC address on forms:
Code:
public static String GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if(ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "";
}
Now this will not work on internet get's but I've read that it will work with Local networks. Is that the case? Meaning I can get an address of a PC that is on the network using a web application or I will just get the server IIS name?
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 30th, 2021, 02:47 AM
#2
Re: Just to be sure, get remote IP address
Originally Posted by sapator
Hi.
I'm asking to be sure.
I do this to get a PC address on forms:
Code:
public static String GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if(ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "";
}
Now this will not work on internet get's but I've read that it will work with Local networks. Is that the case? Meaning I can get an address of a PC that is on the network using a web application or I will just get the server IIS name?
Thanks.
Dns.GetHostName will return whatever IPAddresses are assigned to the local machine, these addresses are normally local to the network and not directly reachable over the internet. Your code will actually only return the 1st address associated with the local machine, rather than all of them the following might be better as it returns all of them
Code:
static IEnumerable<String> GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
yield return ip.ToString();
}
}
}
If you are trying to get the external address of you local machine you might be better making an http request to something like http://checkip.dyndns.org/ as this will report on your public address (typically your public router or firewall). Anything on the internet could then reach that, although there is a good chance you would then need to configure the public firewall / router / whatever to allow connections in to your actual pc.
-
Oct 30th, 2021, 11:03 AM
#3
Re: Just to be sure, get remote IP address
Hey.
I'm actually trying to get whatever machine local address the PC is using. So mine is 192.168.1.1 when a person on another building on the local network hits the site it will be 192.168.20.1 etc.
So simulate a whatismyip thing. I'm thinking it might be able to be done with javascript but from what I've read the browsers might or might not support it.
From what I'm reading I believe something more viable would be this: https://www.c-sharpcorner.com/blogs/...ress-in-mvc-30
Will have to try it out tho and since next week I'm working from home, err, it will take a while.
Edit: Tested it locally and it returns ::1 , so I don't have a clue on what will return remotely.
Last edited by sapator; Oct 30th, 2021 at 12:18 PM.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Dec 30th, 2021, 05:52 AM
#4
Re: Just to be sure, get remote IP address
This will return the server ip address where the application is running not the client IP address. You can try something like
Code:
string clientIp = (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ??
Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
This will get the details from browser. Also you can try printing the values of Request.ServerVariables and see if that has more useful values
Please mark you thread resolved using the Thread Tools as shown
-
Jan 26th, 2022, 07:34 AM
#5
New Member
Re: Just to be sure, get remote IP address
This should help
Code:
private string GetUserIP()
{
string ipList = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipList))
{
return ipList.Split(',')[0];
}
return Request.ServerVariables["REMOTE_ADDR"];
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|