Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the IP address of server in ASP.NET?

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.

like image 457
jergason Avatar asked Mar 14 '09 19:03

jergason


People also ask

How do I get client IP address in asp net core?

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.

How can I get IP address in C#?

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.

How do I find my host IP?

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.


1 Answers

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

like image 169
TStamper Avatar answered Oct 03 '22 23:10

TStamper