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;
       base.Response.Write(address);
   }


My Ip Address is 202.164.45.14
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 192.168.3.1

I also tried
HttpRequest.ServerVariables["REMOTE_ADDR"]


but it also gave same result.

Thanks,
Richa Sharma
Posted
Updated 1-Aug-12 4:05am
v2

::1 is your address. Just as 127.0.0.1 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.


Thanks,
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