Get IP Address In ASP.NET

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:

private void GetIpValue(out string ipAdd)  
{  
    ipAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  
    if (string.IsNullOrEmpty(ipAdd))  
    {  
        ipAdd = Request.ServerVariables["REMOTE_ADDR"];  
    }  
    else  
    {  
        lblIPAddress.Text = ipAdd;  
    }  
}

Second Method to get IP Address

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:

private void GetIpAddress(out string userip)  
{  
    userip = Request.UserHostAddress;  
    if (Request.UserHostAddress != null)  
    {  
        Int64 macinfo = new Int64();  
        string macSrc = macinfo.ToString("X");  
        if (macSrc == "0")  
        {  
            if (userip == "127.0.0.1")  
            {  
                Response.Write("visited Localhost!");  
            }  
            else  
            {  
                lblIPAdd.Text = userip;  
            }     
        }  
    }  
}

Default.aspx

<!DOCTYPE html>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Get IP Address</title>  
    <link href="Files/css/bootstrap.css" rel="stylesheet" />  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h1><strong>Your IP Address : </strong></h1>  
        <br/>  
        <h1><strong>Method 1 : </strong><asp:Label ID="lblIPAddress" CssClass="label label-primary" runat="server" Text="Label"></asp:Label></h1>  
        <br />  
        <h1><strong>Method 2 : </strong><asp:Label ID="lblIPAdd" CssClass="label label-info" runat="server" Text="Label"></asp:Label></h1>  
    </div>  
    </form>  
</body>  
</html>

Call above methods in Page_Load event, as soon as the page loads you will get an output like this.

ip address in ASP.Net

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#.

You can get more about a visitors, its browser and other details. Learn more here: Get Client IP Address In ASP.NET MVC 


Similar Articles