How to Get Users IP Address using C#, VB.NET with Example?

Here I would like to explain that how to get users / client IP address in 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.Let us see How to Get Users IP Address using C#, VB.NET with Example



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 see the 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.

Leave a Reply

Your email address will not be published. Required fields are marked *