In this article I will show you how you how to get a user's
client IP address in ASP.NET using C#.Net.
Some of my previous articles are as
follows: How
to Get the Full Path of FileUpload Control in Asp.Net, How
To Get File Size Uploaded By FileUpload Control Using C# In Asp.Net, Asp.Net-Validating
Radio Button List using Required Filed Validator | How to Use Required Field
Validator For Radiobuttonlist in Asp.Net.
Now here is the code for getting the client ip address in
asp.net.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RetriveUserIpAddressInAspNet.aspx.cs" Inherits="ProjectDemo_Asp.et.RetriveUserIpAddressInAspNet"
%>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Client Id Address:
<asp:Label ID="lblIPddress" runat="server" Text=""
style="color: #FF0066; font-size: x-large"></asp:Label>
</div>
</form>
</body>
</html>
|
C#.Net code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ProjectDemo_Asp.et
{
public partial class RetriveUserIpAddressInAspNet : System.Web.UI.Page
{
public
void Page_Load(Object
sender, EventArgs e)
{
//Print
the time when the page loaded initially
lblIPddress.Text=
GetLanIPAddress().Replace("::ffff:",
"");
}
/*
Method to get the
IP Address of the User
*/
public
String GetLanIPAddress()
{
//The
X-Forwarded-For (XFF) HTTP header field is a de facto standard for
identifying the originating IP address of a
//client
connecting to a web server through an HTTP proxy or load balancer
String
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if
(string.IsNullOrEmpty(ip))
{
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
return
ip;
}
}
}
|
Now we have some please check the output.
The above address is my localhost IP
0 comments:
Please let me know your view