Firstly, Request.ServerVariables["HTTP_X_FORWARDED_FOR"] is a correct way to retrieve client IP address. The below code is an approach for getting Clinet IP.
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else// not using proxy or can't get the Client IP
{
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
As far as I know, you can not always get the Client IP through a proxy.
1. Some proxys can not send the client IP to you.
2. Some Clients can prevent from sending IP by "header_access deny".
In above scenarios, HTTP_VIA and HTTP_X_FORWARDED_FOR are null. Meanwhile, you can just retrieve the proxy IP by "REMOTE_ADDR", but you can't get the real Client IP.
Based on your description, HTTP_X_FORWARDED_FOR is null. So the reason on it is either preventing from sending clinet IP by proxy or setting "header_access deny" by client sides.
popperyhooop
Member
68 Points
195 Posts
get IP location?
Aug 01, 2012 06:57 AM|LINK
hi anyone help me how can i get my ip location? exact location using or detect only the ip of my device? tahnks im using asp.net c#
Rajneesh Ver...
All-Star
36903 Points
6766 Posts
Re: get IP location?
Aug 01, 2012 07:01 AM|LINK
Try as:
HttpRequest currentRequest = HttpContext.Current.Request; string ipAddress = currentRequest.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (ipAddress == null || ipAddress.ToLower() == "unknown") ipAddress = currentRequest.ServerVariables["REMOTE_ADDR"]; return ipAddress;www.rajneeshverma.com
Keep Forums Clean || Use Alert Moderators.
popperyhooop
Member
68 Points
195 Posts
Re: get IP location?
Aug 01, 2012 07:08 AM|LINK
should i change
REMOTE_ADDR" ? to an IP? or should i put a direct ip there?Vivek J Pand...
Participant
922 Points
257 Posts
Re: get IP location?
Aug 01, 2012 07:17 AM|LINK
private string GetUserIP() { return Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"]; }try this
http://msdn.microsoft.com/en-us/library/system.web.httprequest.userhostaddress.aspx
--Vivek
Rajneesh Ver...
All-Star
36903 Points
6766 Posts
Re: get IP location?
Aug 01, 2012 07:26 AM|LINK
No need to change the code try it same as written
www.rajneeshverma.com
Keep Forums Clean || Use Alert Moderators.
india.999
Member
225 Points
116 Posts
Re: get IP location?
Aug 01, 2012 07:31 AM|LINK
http://www.aspsnippets.com/Articles/Find-Visitors-Geographic-Location-using-IP-Address-in-ASP.Net.aspx
ashok.amsama...
Participant
770 Points
158 Posts
Re: get IP location?
Aug 01, 2012 08:13 AM|LINK
look at these intersting things
Hi,
Firstly, Request.ServerVariables["HTTP_X_FORWARDED_FOR"] is a correct way to retrieve client IP address. The below code is an approach for getting Clinet IP.
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else// not using proxy or can't get the Client IP
{
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
As far as I know, you can not always get the Client IP through a proxy.
1. Some proxys can not send the client IP to you.
2. Some Clients can prevent from sending IP by "header_access deny".
In above scenarios, HTTP_VIA and HTTP_X_FORWARDED_FOR are null. Meanwhile, you can just retrieve the proxy IP by "REMOTE_ADDR", but you can't get the real Client IP.
Based on your description, HTTP_X_FORWARDED_FOR is null. So the reason on it is either preventing from sending clinet IP by proxy or setting "header_access deny" by client sides.
Hope this can help.
refer: http://forums.asp.net/t/1255251.aspx/1
Ashok
"MARK AS ANSWER IF MY POST HELPS YOU"
prabu.raveen...
Contributor
5020 Points
955 Posts
Re: get IP location?
Aug 01, 2012 09:08 AM|LINK
The below url may help you,
http://www.codeproject.com/Articles/15366/Finding-Countries-and-Cities-Using-IP-Address
gaurabchatte...
Participant
1655 Points
325 Posts
Re: get IP location?
Aug 01, 2012 09:28 AM|LINK
You can read the following article
http://www.dotnetcurry.com/ShowArticle.aspx?ID=325
http://www.codeproject.com/Articles/62488/Easy-to-Track-the-Geographical-Location-Based-on-I
http://www.codeproject.com/Articles/15366/Finding-Countries-and-Cities-Using-IP-Address
Microsoft Technologies
chrislim2888
Member
50 Points
11 Posts
Re: get IP location?
Aug 03, 2012 08:12 AM|LINK
This link may give you some idea of the implementation http://www.ip2location.com/developers