Friday, January 8, 2016

Asp.net: Get public IP address of visitors

In this article I am going to explain how to get the public IP address of visitors


Description:
Many times we needed the IP address of visitors. To  get the IP address of  visitors use the below given snippets.

Implementation:
C# Code:


protected void Page_Load(object sender, EventArgs e)
    {
string ipaddress;
        ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (ipaddress == "" || ipaddress == null)
            ipaddress = Request.ServerVariables["REMOTE_ADDR"];
        lblipaddress.Text = ipaddress;
}

VB.net Code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim ipaddress As String
        ipaddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If ipaddress = "" OrElse ipaddress Is Nothing Then
            ipaddress = Request.ServerVariables("REMOTE_ADDR")
        End If
        lblipaddress.Text = ipaddress
    End Sub

Build and run the application. To test it uploads it to server.

   In this article we have learn how to get the public IP address of visitors. I hope you enjoyed this article. Please post you comment, query and feedback about this article. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email. 

No comments:

Post a Comment