Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get a client's ip address with ASP.NET

Newbie
 
Join Date: Dec 2005
Posts: 3
#1: Dec 27 '05
I want to fetch client's ip address as soon as he accesses my web-site built in asp.net.Then i want to store it in my database.
Is there any method to achieve this target in asp.net application.
If you could help me i will be very thankful to you.

KUB365's Avatar
Administrator
 
Join Date: Jul 2005
Location: Portland, OR
Posts: 969
#2: Dec 27 '05

re: How to get a client's ip address with ASP.NET


ASP .Net web page:
Expand|Select|Wrap|Line Numbers
  1. Dim strClientIP As String
  2. strClientIP = Request.UserHostAddress()
  3. Response.Write(strClientIP)
  4.  
ASP .NET web services:
Expand|Select|Wrap|Line Numbers
  1. Context.Request.ServerVariables ["REMOTE_ADDR"]
  2.  
Newbie
 
Join Date: Jun 2007
Posts: 1
#3: Jun 10 '07

re: How to get a client's ip address with ASP.NET


or you can try:
Expand|Select|Wrap|Line Numbers
  1. string ip;
  2.  
  3. ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
  4. if(ip==string.Empty)
  5. {
  6. ip=Request.ServerVariables("REMOTE_ADDR");
  7. }
REMOTE_ADDR does not always provide the users IP but rather the ISPs' IP address so first test HTTP_X_FORWARDED_FOR as this one is the real user IP.
Newbie
 
Join Date: Feb 2009
Posts: 1
#4: Feb 16 '09

re: How to get a client's ip address with ASP.NET


This is very helpful for me. Many thanks
Newbie
 
Join Date: Jun 2009
Posts: 2
#5: Jun 24 '09

re: How to get a client's ip address with ASP.NET


Another important note to this

the HTTP_X_FORWARDED_FOR may contain an array of IP, this can happen if you connect through a proxy.
What also happens when this happens is that the REMOTE_ADDR may contain the proxy IP.

to avoid this problem you can parse the HTTP_X_FORWARDED_FOR for the last entery IP.

this can easily be done using...
Expand|Select|Wrap|Line Numbers
  1. ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR") ;
  2. if (!string.IsNullOrEmpty(ip))
  3. {
  4.    string[] ipRange = ip.Split(',');
  5.    int le = ipRange.Length - 1;
  6.    string trueIP = ipRange[le];
  7. }
  8. else
  9. {
  10.    ip=Request.ServerVariables("REMOTE_ADDR");
  11. }
This code example is C#.NET but the method is the same

What this does is check if we have anything in the HTTP_X_FORWARDED_FOR, and if we do we take out the last entry (works if only one entry is found aswell)
if we don't have an HTTP_X_FORWARDED_FOR we set the IP from the REMOTE_ADDR instead
Newbie
 
Join Date: Jul 2009
Posts: 2
#6: Jul 7 '09

re: How to get a client's ip address with ASP.NET


only one thing...Servervariables is the NameValueCollection type so its items cannot be accesed by doing ServerVariables(ItemName) if C# is used for coding.
Instead please use... ServerVariables[ItemName] that doesn't hurt
Newbie
 
Join Date: Jun 2009
Posts: 2
#7: Jul 8 '09

re: How to get a client's ip address with ASP.NET


Yes of cause. My misstake.

there were also some concern about the order of the X-FORWARDED-FOR addresses.

My code gets the last entry in the string.
for the first entry simply use
Expand|Select|Wrap|Line Numbers
  1.  if (!string.IsNullOrEmpty(ip))
  2.  {
  3.     string[] ipRange = ip.Split(',');
  4.     string trueIP = ipRange[0];
  5.  }
instead of
Expand|Select|Wrap|Line Numbers
  1.  if (!string.IsNullOrEmpty(ip))
  2.  {
  3.     string[] ipRange = ip.Split(',');
  4.     int le = ipRange.Length - 1;
  5.     string trueIP = ipRange[le];
  6.  }
Newbie
 
Join Date: Jul 2009
Posts: 2
#8: Jul 8 '09

re: How to get a client's ip address with ASP.NET


also remember that those values are a comma+space separated list so you could add string trueIP = ipRange[0].Trim(); in order to avoid spaces in fornt or in the back of your result, but that is just a bit picky from my side... sorry about that
Reply