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
![]() |
ASP.Net Get IP Address |
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>
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;
}
}
}
Conclusion : It 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!
C# Get IP Address - ASP.Net
Reviewed by Ravi Kumar
on
6:39 PM
Rating:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD0muK4sIFEfZeJ71w3TRRkehsXIxsNq77CUH8PQIqzGyqiNaTrrBg99Eh2XmX1ne_MCbYBi9Q0lKvC6bRcCDdFpGX3FlhCx_pVHs8yjpb_Rtx1DUR8USSJoEYjg1pkan3TCROH2Al3b3Q/s72-c/ip-address.jpg)
really gud code.. solved my prob.
ReplyDeleteIt's Working,Thangs For Uploading.
ReplyDeleteyou're welcome .. :)
Delete