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

Get Users IP Address in Asp.Net using C#, VB.NET with Example

Apr 11, 2016

Here we will learn how to get users / client IP address in using c# and with example or retrieving user's IP address in using c# and or how to get IP address of visitors machine in using c# and with example.

In previous articles I explained JavaScript Get User IP Address, City, Latitude, Longitudehow to find client machine IP using jquery in, show users current location in google map using latitude and longitude in, show google map with latitude & longitude in and many articles relating to jQuery. Now I will explain how to get users ip address or client IP address of machine in using c# and

To get user / client ip address of machine in first create new application in visual studio then open aspx page and write the code as shown following.

<html xmlns="">
<head runat="server">
<title>Get visitors machine IP in</title>
<form id="form1" runat="server">
<asp:Label ID="lblIP" runat="server" Font-Bold="true" Font-Size="XX-Large" ForeColor="#529E00"/>

Now open code behind file and write the code like as shown below

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;


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 ip address behind the proxies or routers then REMOTE_ADDR will return IP Address of router, not the user’s machine IP because of that first we need to check with HTTP_X_FORWARDED_FOR. In case if users machine IP address behind a proxy server then his machine’s IP Address will appended to Proxy Server’s IP Address.


Following is the sample demo of ip address of user

Your IP Address:

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

Get the latest,, 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


Sunil Pal said...

u made my day !!!

Unknown said...


Anonymous said...


Unknown said...

Thanks Sir..

kkeyanece said...

Its Nice but this only get the router ip addresses i mean public address not private ip of client machine, what can i do? Please drop out my trouble

Ashu said...

it not work show ip address ::1

Unknown said...

it not works show ip address::1

Unknown said...

Hi, How to save ip to the database. Please help me!

Dinesh said...

it not working it shows me ::1 as ip address

varun pujara said...

While running this application on your machine locally i.e. in Visual Studio, the IP Address will show as or ::1. This happens because in such case the Client and Server both are the same machine. Thus no need to worry when you deploy it on a Server, you will see the results.

Give your Valuable Comments

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

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