show-notice
hide-notice

Saturday, 13 July 2013

How to get IP Address of Visitor's Machine in asp.net


Introduction

Many times there’s a query to get the IP Address of the client machine from which the user visited the website. Hence I decided to post this snippet. The following snippet gets the IP address of the machine from which the client visited the website.

When users are behind any proxies or routers the REMOTE_ADDR returns the IP Address of the router and not the client user’s machine. Hence first we need to check HTTP_X_FORWARDED_FOR, since when client user is behind a proxy server his machine’s IP Address the Proxy Server’s IP Address is appended to the client machine’s IP Address. If there are multiple proxy servers the IP Addresses of all of them are appended to the client machine IP Address. Hence we need to first check HTTP_X_FORWARDED_FOR and then REMOTE_ADDR. 

Example

Aspx File



<%@ page language="C#" autoeventwireup="true" codefile="How-to-get-IP-Address-of-Visitor-c.aspx.cs"
    inherits="CSharp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>IP To Country Example</title>
    <style type="text/css">
        .text
        {
            font-family: Arial;
            font-size: 11pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="width: 173px">
                    <asp:label id="Label1" runat="server" text="Country : " cssclass="text" width="87px" />
                </td>
                <td style="width: 188px">
                    <asp:label id="lblCity" runat="server" cssclass="text" width="186px" />
                </td>
            </tr>
            <tr>
                <td style="width: 173px">
                    <asp:label id="Label2" runat="server" text="Region : " cssclass="text" width="87px" />
                </td>
                <td style="width: 188px">
                    <asp:label id="lblRegion" runat="server" cssclass="text" width="186px" />
                </td>
            </tr>
            <tr>
                <td style="width: 173px">
                    <asp:label id="Label3" runat="server" text="Country : " cssclass="text" width="87px" />
                </td>
                <td style="width: 188px">
                    <asp:label id="lblCountry" runat="server" cssclass="text" width="186px" />
                </td>
            </tr>
            <tr>
                <td style="width: 173px">
                    <asp:label id="Label4" runat="server" text="CountryCode : " cssclass="text" width="87px" />
                </td>
                <td style="width: 188px">
                    <asp:label id="lblCountryCode" runat="server" cssclass="text" width="186px" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

C#




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;

using System.Xml;



public partial class CSharp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Get IP Address
        string ipaddress;
        ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (ipaddress == "" || ipaddress == null)
            ipaddress = Request.ServerVariables["REMOTE_ADDR"];

        DataTable dt = GetLocation(ipaddress);
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                lblCity.Text = dt.Rows[0]["City"].ToString();
                lblRegion.Text = dt.Rows[0]["RegionName"].ToString();
                lblCountry.Text = dt.Rows[0]["CountryName"].ToString();
                lblCountryCode.Text = dt.Rows[0]["CountryCode"].ToString();
            }
            else
            {

            }
        }

    }
    private DataTable GetLocation(string ipaddress)
    {
        //Create a WebRequest
        WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/" + ipaddress);

        //Create a Proxy
        WebProxy px = new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true);

        //Assign the proxy to the WebRequest
        rssReq.Proxy = px;

        //Set the timeout in Seconds for the WebRequest
        rssReq.Timeout = 2000;
        try
        {
            //Get the WebResponse
            WebResponse rep = rssReq.GetResponse();

            //Read the Response in a XMLTextReader
            XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

            //Create a new DataSet
            DataSet ds = new DataSet();

            //Read the Response into the DataSet
            ds.ReadXml(xtr);
            return ds.Tables[0];
        }
        catch
        {
            return null;
        }
    }
}




SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com