How to get IP Address of the client using Asp.Net C#,VB ?

IntroductionIn this article i will explain how to read or fetch IP Address of the user i.e. client using 

In previous article i explained How to read mac address in and How to get browser name, version, type, operating system in and Get Title, Description and Keywords Meta tags from URL in and Prevent duplicate data entry on page refresh in and Enable browser javascript in

Description: Sometimes you may want to get the IP Address of the client in ASP.NET. 
In that case you can use the property "UserHostAddress" of the current Request object or by checking "REMOTE_ADDR" and "HTTP_X_FORWARDED_FOR".  I have mentioned two ways to read user IP Address. You can use any of the two.

Asp.Net C# Code to read IP Address of the client

  protected void Page_Load(object sender, EventArgs e)
        Response.Write("IP Address: " + GetIPAddress());
        // Response.Write("IP Address: " + GetIPAddress2()); 

    protected string GetIPAddress()
        HttpRequest request = HttpContext.Current.Request;
        return request.UserHostAddress;
    protected string GetIPAddress2()
        string IPAddress = string.Empty;
        IPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(IPAddress))
            IPAddress = Request.ServerVariables["REMOTE_ADDR"];
        return IPAddress;

Asp.Net VB Code to read IP Address of the client

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Response.Write("IP Address: " & GetIPAddress())
        'Response.Write("IP Address: " & GetIPAddress2())
    End Sub
    Protected Function GetIPAddress() As String
        Dim request As HttpRequest = HttpContext.Current.Request
        Return request.UserHostAddress
    End Function

    Protected Function GetIPAddress2() As String
        Dim IPAddress As String = String.Empty
        IPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If String.IsNullOrEmpty(IPAddress) Then
            IPAddress = Request.ServerVariables("REMOTE_ADDR")
        End If
        Return IPAddress
    End Function

Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Next Post »


Click here for comments
September 28, 2013 ×

i want a Date Time Picker me

September 29, 2013 ×

I will post an article on use of DateTimePicker as soon as stay connected for more updates..

March 14, 2014 ×

Hello choco..thanks for appreciating my work..i am glad you found this article helpful..stay connected and keep reading..:)


If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..