Get the visitors IP address in ASP.NET
This article helps you to know how to get the IP address of visitors to your site in ASP.NET.
Every visitor to your site or web application has an IP address and many times you want to get it for the purpose of security logging, or perhaps tracing. This article helps you to know how to get the IP address of visitors of your site.
As you know that when users are behind any routers or proxies the REMOTE_ADDR returns the IP Address of the router and not the client user's machine. Hence first of all we have to check HTTP_X_FORWARDED_FOR, since when client user is behind a proxy server its machine's IP Address and Proxy Server's IP Address is appended to the client machine's IP Address. So, we need to check HTTP_X_FORWARDED_FOR and then REMOTE_ADDR.
Lets see the code snippets to get the visitors IP address.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="IP_Address" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head id="Head1" runat="server">
<form id="form1" runat="server">
<asp:TextBox ID="txtIPAddress" runat="server" Width="205px"></asp:TextBox
Partial Class IP_Address
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim nowip As String
nowip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If nowip = "" Then
nowip = Request.ServerVariables("REMOTE_ADDR")
If txtIPAddress.Text = "" Then
txtIPAddress.Text = nowip
Run the above code and you will get IP address of the visitor to your site.