Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old December 27th, 2005, 08:04 AM
Newbie
 
Join Date: Dec 2005
Posts: 3
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.
  #2  
Old December 27th, 2005, 08:12 AM
KUB365's Avatar
Administrator
 
Join Date: Jul 2005
Location: Portland, OR
Posts: 942
Provided Answers: 1

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.  
  #3  
Old June 10th, 2007, 06:44 AM
Newbie
 
Join Date: Jun 2007
Posts: 1

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.

Last edited by Frinavale; July 7th, 2009 at 09:04 PM. Reason: Added code tags. Please post code in [code] [/code] tags.
  #4  
Old February 16th, 2009, 03:19 AM
Newbie
 
Join Date: Feb 2009
Posts: 1

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


This is very helpful for me. Many thanks
  #5  
Old June 24th, 2009, 01:40 PM
Newbie
 
Join Date: Jun 2009
Posts: 2

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
  #6  
Old July 7th, 2009, 08:44 PM
Newbie
 
Join Date: Jul 2009
Posts: 2

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
  #7  
Old July 8th, 2009, 07:52 AM
Newbie
 
Join Date: Jun 2009
Posts: 2

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.  }
  #8  
Old July 8th, 2009, 01:36 PM
Newbie
 
Join Date: Jul 2009
Posts: 2

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get client IP address assigned by ISP Mike answers 15 October 24th, 2008 06:05 PM
Problem: GET/POST/ViewState and swapping CLIENT IP address in one session Tony answers 1 March 31st, 2006 04:35 PM
Client IP Address or PC Name Homer answers 7 November 22nd, 2005 02:48 PM
Client IP Address or PC Name Homer answers 8 July 21st, 2005 06:56 PM