Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 1-Aug-12 3:04am
Edited 1-Aug-12 3:05am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

::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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 645
1 OriginalGriff 335
2 CPallini 250
3 PIEBALDconsult 150
4 Fredrik Bornander 130
0 OriginalGriff 5,835
1 Sergey Alexandrovich Kryukov 5,198
2 CPallini 4,740
3 George Jonsson 3,167
4 Gihan Liyanage 2,470


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 17 Dec 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100