Introduction:
In this article I will explain how to find system or client machine IP address from which user visited the website in asp.net behind proxy in c# and vb.net.
In this article I will explain how to find system or client machine IP address from which user visited the website in asp.net behind proxy in c# and vb.net.
Description:
In previous posts I explained JavaScript Get User IP Address, City, Latitude, Longitude, how to find client machine IP using jquery in asp.nt relating to JQuery. Now I will explain how to get system or client IP address of machine from which the user visited the website in asp.net in c# and vb.net. To implement this one write the code as shown below
In previous posts I explained JavaScript Get User IP Address, City, Latitude, Longitude, how to find client machine IP using jquery in asp.nt relating to JQuery. Now I will explain how to get system or client IP address of machine from which the user visited the website in asp.net in c# and vb.net. To implement this one write the code as shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Get visitors machine IP in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblIP" runat="server" Font-Bold="true" Font-Size="XX-Large" ForeColor="#529E00"/>
</div>
</form>
</body>
</html>
|
Now write the following code in code behind
C# Code
protected void Page_Load(object sender, EventArgs e)
{
string IPAdd = string.Empty;
IPAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(IPAdd))
IPAdd = Request.ServerVariables["REMOTE_ADDR"];
lblIP.Text = IPAdd;
}
|
VB.NET Code
Partial Class Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim IPAdd As String = String.Empty
IPAdd = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If String.IsNullOrEmpty(IPAdd) Then
IPAdd = Request.ServerVariables("REMOTE_ADDR")
End If
lblIP.Text = IPAdd
End Sub
End Class
|
If you observe above code when users behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to checkHTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address.
Demo
Your IP Address:
180.151.55.134 |