CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to get Client Computer/PC IP Address and name in ASP.Net ?
Submitted By Bala Murugan
On 8/18/2010 10:36:21 AM
Tags: ASP.Net,CodeDigest  

How to get Client Computer/PC IP Address and Name in ASP.Net ?

 

At times, we may like to log the visitor informations like ip address and machine name from which he or she has visted our website. The browser will populate many information including the ipaddress of the machine the visitor is visiting with each http request it is making to a website. This information can be used by the webserver to to know more about the request origin and etc.

 

In ASP.Net, once it receives the request it forms a HttpContext object which will have all the information about the request. This HttpContext object exposes Request object that has some specific information about the Request which will include the IP address of the requestor. Hence, to get the ipaddress of the visitor use UserHostAddres property of Request object.

 

To get the hostname/computer name, you can use the GetHostEntry() method of Dns class in System.Net namespace. The GetHostEntry() will accept the ipaddress will resolve it to a host name by querying the DNS server.

 

The below little code snippet will help us to get the ip address and name of the client who is accessing our asp.net site.
 
protected void Page_Load(object sender, EventArgs e)
    {       
        Response.Write("Your IP Address: "+ Request.UserHostAddress+"<BR>");
        Response.Write("Your Computer Name: "+ System.Net.Dns.GetHostEntry(Request.UserHostAddress).HostName);
    }


 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..