Latest

random

C# Get IP Address - ASP.Net

Finding IP Address of the Client Machine


While developing asp.net web applications many time's there is a query to get ip address of the client machine from which the user visited the website. 

So In this article I covered how to retrieve the client's IP address or track your website visitor's IP Address in C#. To retrieve the ip address from the client there are basically two scenarios :- 
  • Find IP address of client's machine behind LAN
  • Find IP address behind proxy or client machine
IP address, UserHostAddress property in the ASP.NET,  Retrieving User's IP Address, get the IP Address, How to get user IP Address in asp.net,get the IP address in ASP.NET,get an IP Address,client's IP address in ASP.NET,how to find the IP address of the client
ASP.Net Get IP Address
To get the IP address in asp.net web application find the code below :-

Default.aspx :
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Find IP Address and Host Name</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-weight:bold; border:1px;">
        Your IP Address :- <asp:Label ID="lblIPAddress" runat="server" Text=""></asp:Label><br /><br />
        Your Computer/Host Name :- <asp:Label ID="lblHostName" runat="server" Text=""></asp:Label>
        <br /><br />
      Your IP Address Behind Proxy :- <asp:Label ID="lblIPBehindProxy" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

client IP,ip address,ip address of my computer,hack ip address,get ip address,ip information,what is a ip address on a computer, aspnet,c#

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

// Include System.Net Namspace
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // To find IP address of a machine behind LAN you can use this code
        string strHostName = Dns.GetHostName();     
        IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);

        lblIPAddress.Text  = Convert.ToString(ipEntry.AddressList[1]);
        lblHostName.Text  = Convert.ToString(ipEntry.HostName);


        //Find IP Address Behind Proxy Or Client Machine In ASP.NET
        string IPAdd = string.Empty;
        IPAdd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (string.IsNullOrEmpty(IPAdd))
        {
            IPAdd = Request.ServerVariables["REMOTE_ADDR"];
            lblIPBehindProxy.Text = IPAdd;
        }
    }    
}

ConclusionIt was fun in learning and writing an article on C# Getting IP Address - ASP.Net. I hope this article will be helpful for enthusiastic peoples who are eager to learn and implement some interesting stuffs in new technology. 
Please feel free to comment your opinion about this article or whatever you feel like telling me. Also if you like this article, don't forget to share this article with your friends. Thanks!

3 comments:

All Rights Reserved by Etechpulse © 2015 - 2016
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.