Click here to Skip to main content
14,775,008 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi All,

I have to fetch the ip address client machine.
for this i use the below code:
protected void Application_BeginRequest(object sender, EventArgs e)
       HttpRequest request = base.Request;
       string address = request.UserHostAddress;

My Ip Address is
But when i run the above code on my local system, it returns ::1
And when i deploy it on my internal server it is giving

I also tried

but it also gave same result.

Richa Sharma
Updated 1-Aug-12 4:05am

::1 is your address. Just as is the loopback address in IPv4, ::1 is the loopback on IPv6
see Localhost[^]
Unless you need a "properly" resolved IP I'd keep your code, once it had been tested from another machine on the network.
Thanks all for your interest in this question.
But the problem is firewalls. When i deploy this on server, it is working fine.

Richa Sharma
HttpContext context = ((HttpApplication)sender).Context;
string currentIP = context.Request.UserHostAddress;
if (!IsIpValid(currentIP))
context.Response.StatusCode = 403;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900