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 asp.net. 

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

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. 
Previous
Next Post »

4 comments

Click here for comments
Anonymous
admin
September 28, 2013 ×

i want a Date Time Picker ..help me

Reply
avatar
September 29, 2013 ×

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

Reply
avatar
March 14, 2014 ×

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

Reply
avatar

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