How do I get the IP address of the server that calls my ASP.NET page? I have seen stuff about a Response object, but am very new at c#. Thanks a ton.
Client IP address can be retrieved via HttpContext. Connection object. This properties exist in both Razor page model and ASP.NET MVC controller. Property RemoteIpAddress is the client IP address.
By passing the hostname to GetHostByName() method we will get the IP Address. This method returns a structure of type hostent for the specified host name. AddressList[0] gives the ip address and ToString() method is used to convert it to string.
First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all. Your ip address will be the IPv4 address.
This should work:
//this gets the ip address of the server pc public string GetIPAddress() { IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); // `Dns.Resolve()` method is deprecated. IPAddress ipAddress = ipHostInfo.AddressList[0]; return ipAddress.ToString(); }
http://wec-library.blogspot.com/2008/03/gets-ip-address-of-server-pc-using-c.html
OR
//while this gets the ip address of the visitor making the call HttpContext.Current.Request.UserHostAddress;
http://www.geekpedia.com/KB32_How-do-I-get-the-visitors-IP-address.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With