Fetch IP Address - asp.net

I want to get ip address of client machine Like I opened website on my machine which is hosted on any server So i need IP address of my machine not hosted server IP.

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
use above syntax in C# otherwise you will get error like Non-invocable member 'System.Web.HttpRequest.ServerVariables' cannot be used like a method.

''# VB
HttpContext.Current.Request.ServerVariables("REMOTE_ADDR")
// C#
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
This will get the PUBLIC ip address of the client computer. It will NOT get the LAN IP (192.168...)

Related

Fritzbox public ip address with DS-Lite

I am struggeling to connect to my home server that is connected via a Fritzbox router to the internet. I want to connect to the home server from outside of the home net, as it serves as a NAS and provides HTTP(S) services.
The problem is, that I don't understand how to connect to the server over the internet. My Fritzbox is connected to my internet provider via DS-Lite internet connection. As far as I understood, this means that my Fritzbox has no public IPv4 address and therefore the server is not reachable.
Is it still somehow possible to connect to the server?
Reading your question, I can see that there are multiple steps to solve this.
figure out if your internet provider allows you to have incoming connections
I do not know, what a ds lite connection is. Depending on your connection type, e.g. glass fibre, dsl, mobile and your provider incoming connections might be allowed or not. Also specific ports might be forbidden.
Enable port forwarding for incoming connections to your lan server.
Your fritzbox does not know, where to route the incoming connection to.
Make your lan server ip address static. Go to your fritzbox admin page and create a port forwarding rule and map data incoming on port 80(HTTP) and 443(HTTPS) to the lan server ip address.
You can read further here: https://en.wikipedia.org/wiki/Port_forwarding
Figure out the fritzbox's public ip address by checking out this website from within your lan. https://whatismyipaddress.com/
Connect to your server via http(s)://publicip
setup dynamic dns to have a public domain, which you can use instead of the ip address.
Usually private customer internet connections use dynamic ip addresses. So your ip address changes regularly. This is annoying, because you need to lookup the ip address before you can connect again. To avoid this issue, you can use a dynamic dns provider to give you a domain name, which you can use instead of the public ip address. Your fritzbox should have this kind of functionality already. If not, you can also configure it on your server with a cron job.
You can read further here: https://en.wikipedia.org/wiki/Dynamic_DNS
This provider is easy to use and for free: https://freedns.afraid.org/
use the dynamic dns domain name instead othe public to access your server from anywhere
Be aware, that having open connections to your local network gives attack surface from the public internet. So people might steal or delete data on your server or abuse it in other ways.

Access server in local network through domain

i have problem with access to server with domains.
Test url: testpage.example.com.
Server is in local network with port fowarding (80, 443), configured as web server using caddy server as reverse proxy.
Case 1 (using Asus router):
Connected on local network behind router. Server is in the same network as my computer. Everything works like a charm in and out of my network.
Case 2 (using internet provider router):
Connected on local network behind router. Can't access server with domain. Works with direct IP. Outside network, works as in case 1.
I used same server.
Does anyone know why this problem occured? How can i solve it?
Thanks,
David
Two options I can think of:
You could add a record to whatever DNS server you're using in Case 2.
You could write a short script that runs whenever you change network connections to modify your hosts file accordingly.
This happens when the server you are trying to reach "testpage.example.com" resolves to your router's external IP address. Because your public IP address is the same as server's IP address (even though inside your home network you have different private IP addresses) your requests are lost in the ether.
As a workaround you can resolve the testpage.example.com manually on your local machine.
For Windows c:\windows\system32\etc\hosts
For Linux /etc/hosts
testpage.example.com 192.168.1.102 -> private IP of the machine serving the site.

access local server from other networks

I created a localhost server using servlets.
I want to know how to access from other networks(other ip addresses).
what to do for that?
any answers will be helpful for me
you should use public ip address,instead of local ip address to access over the networks.
You can access it by giving IP address of your machine in your application url in place of localhost while accessing it from another machine in the same network.
you can use your ip address instead of localhost.and also need to configure firewall.The firewall can be configured by: choosing the Windows Firewall from the Control Panel, then click on Exceptions -> Add Port and enter name and number: your server, port and leave transport protocol as TCP.
e.g,
http://10.4.0.198:8080/project

I am getting the wrong client IP address

I am running an ASP.NET application. The web server is located on the same system. In the code behind I just want to get the IP address of the requesting client. I am using this code:
Request.UserHostAddress
But I am getting a wrong address: 127.0.0.1. My system IP address is 198.162.0.27.
You are getting the right one. 127.0.0.1 is the loopback IP address, which is mapped in your hosts file to localhost.
If you connect from a remote computer, you will get the remote computers network address.

How to know the IIS server ip for my website access

I have deployed a website in my server, how i know which what my server IP is ?
if i know my server ip , i could access my website from outside
If you know your hostname you can use nslookup to get the ip adress. Alternatively you can find all the ip adresses on the network cards using ipconfig. So either:
nslookup hostname
Or
ipconfig /all
You could also use netstat -n to find the ip adress listening on port 80 (which is the default for http)
First, check with the IIS Console. Right click web site + properties. The tab named "Web Site" shows the "IP Address". This btw could be "All unassigned", so you'd have to do an ipconfig on the server, and deduct IPs explicitly used by other web sites.
Second, beware that this address could be mapped to some other [public] address at the level of a firewall / gateway / WSD device on between the server and the client.
Third (or first), using "ping" or "nslookup" as indicated in other replies, ensure that the name that will be used as part of the url matches the IP discovered above.
Simple way is to ping your hostname. E.g.,
ping microsoft.com
Note you'll need to do this from outside your server's netork, as you may get an internal private IP otherwise.
The IP of the server is the same IP as the host (machine) where it is located, if the server is in your LAN.
You will be able to access your website from outside if your proxys allows it.
I don't really understand what you are talking about saying "my server".
try ping servername and/or ping url
Do it in the code..
Request.ServerVariables("LOCAL_ADDR")
OR
String name = "";
name = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(name);
foreach (IPAddress ipaddress in ipEntry.AddressList)
{
Response.Write("IP : " + ipaddress.ToString());
}

Resources