Get The IP Address In ASP.NET

In this article, you'll learn how to get the IP address of a visitor to your website using ASP.NET and C#.

In this article, you'll learn how to get a website visitor's IP address in ASP.NET using two different methods.


What is an IP Address?

According to Wikipedia, An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. An IP address serves two principal functions: host or network interface identification and location addressing. Its role has been characterized as follows: "A name indicates what we seek. An address indicates where it is. A route indicates how to get there.”

There are two versions of the IP address. The first version is an IPv4 that contains an IP address as a 32-bit number. The newer version is developed as IPv6, which takes a 128-bit number to store an IP Address.

There are two common ways to get the IP Address of a visitor of a website.

First Method to get IP address in ASP.NET

The first method of getting an IP Address is using “HTTP_X_FORWARDED_FOR” and “REMOTE_ADDR”. Where, “X_FORWARDED_FOR” is the HTTP header field for identifying originating IP Address of the client who is connected to Internet and “REMOTE_ADDR” returns the IP address of the remote machine.

Code to get IP Address using Method 1:
  1. private void GetIpValue(out string ipAdd)  
  2. {  
  3.     ipAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  4.   
  5.     if (string.IsNullOrEmpty(ipAdd))  
  6.     {  
  7.         ipAdd = Request.ServerVariables["REMOTE_ADDR"];  
  8.     }  
  9.     else  
  10.     {  
  11.         lblIPAddress.Text = ipAdd;  
  12.     }  
  13. }  
Second Method:

The second method of getting an IP address is using a built-in functionality of ASP.NET. Here we use the Request property of the Page class, which gets an object of HttpRequest class for the requested page. The HttpRequest is a sealed class that enables ASP.NET to read the HTTP values sent by the client browser during a Web request. We access the UserHostAddress property of HttpRequest class to get the IP Address of the visitor.

Code to get IP Address using Method 2:
  1. private void GetIpAddress(out string userip)  
  2. {  
  3.     userip = Request.UserHostAddress;  
  4.     if (Request.UserHostAddress != null)  
  5.     {  
  6.         Int64 macinfo = new Int64();  
  7.         string macSrc = macinfo.ToString("X");  
  8.         if (macSrc == "0")  
  9.         {  
  10.             if (userip == "127.0.0.1")  
  11.             {  
  12.                 Response.Write("visited Localhost!");  
  13.             }  
  14.             else  
  15.             {  
  16.                 lblIPAdd.Text = userip;  
  17.             }     
  18.         }  
  19.     }  
  20. }  
Default.aspx
  1. <!DOCTYPE html>  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title>Get IP Address</title>  
  6.     <link href="Files/css/bootstrap.css" rel="stylesheet" />  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <h1><strong>Your IP Address : </strong></h1>  
  12.         <br/>  
  13.         <h1><strong>Method 1 : </strong><asp:Label ID="lblIPAddress" CssClass="label label-primary" runat="server" Text="Label"></asp:Label></h1>  
  14.         <br />  
  15.         <h1><strong>Method 2 : </strong><asp:Label ID="lblIPAdd" CssClass="label label-info" runat="server" Text="Label"></asp:Label></h1>  
  16.     </div>  
  17.     </form>  
  18. </body>  
  19. </html>  
Call above methods in Page_Load event, as soon as the page loads you will get an output like this.

output

Conclusion

These are two methods to get the IP address of a client. In this article, we learned how to get the IP address of a website visitor using ASP.NET and C#.