Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

how to find ip address in asp.net behind proxy in c# vb.net

Jul 25, 2012
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.

Description:
  
In previous posts I explained JavaScript Get User IP Address, City, Latitude, Longitudehow 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.netTo 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 check HTTP_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

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

14 comments :

Rajkumar Palnati said...

Hi Bro,
Nice Post.
But it show the 127.0.0.1 address
this is not my ip-address.
i think it gives server ip address
how to find my ip address and user's ip address when they are loged into my website or application.

Thanks in advance.
I am very daily visitor to your site it helps me a lot.

Anonymous said...

I also face the same problem as @Rajkumar Palnati
Would you please fix the problem

Unknown said...

upload your program to ftp location and then find it.

Unknown said...

Deploy it on IIS Server. If not installed enable it from Add or Remove programs.

srujan said...

http://www.aspdotnet-suresh.com/2013/01/javascript-get-user-details-ip-address.html

whats the difference between above mentioned IP(Above site) and this present IP

Unknown said...

Hi, Im priya ..

Im was using above coding that will be working in my local machine.
if i done on server pc that ill show 127.0.0.1 this dns name address.
but i needed ip address on server pc

Manish said...

Hello Friends,
Try It
using System.Net;

string IP = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();

Anonymous said...
This comment has been removed by the author.
Anonymous said...
This comment has been removed by the author.
uzair said...

it is showing the 127.0.0.1 address
this is not my ip-address.
how to find my ip address and user's ip address when they are loged into my website or application.

mano said...

Nice blog i used the script which you mentioned here in my website and found the ip address of one client who visited my website ....After finding the ip address i easily traced his location just by doing ip search for that ip address through sites like Ip-Details.com they will display details like ip location ,net name ,address and so on but can't trace the exact home address from ip address i can collect only their ISP details from ip address ....

Anonymous said...

its true

Anonymous said...

Hi tis swatheeswaran..pls contact me...if any doubt
9993808208

Anonymous said...

no use using this code, friends try with Javascript, u can get the IP, i tried and got the query too.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.