Retreive User’s IP Address in ASP.NET

There may be a requirement of getting the user’s IP address using ASP.NET and C# or VB .NET when a user logs in to the webpage.

This can be achieved easily with the .NET Framework.

The IP Address can be retreived in the following ways.

String str = HttpContext.Current.Request.UserHostAddress;

or

HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

The REMOTE_ADDR usually provides the IP Address of the Internet Service Provider in some cases.As a result it may be required to test with HTTP_X_FORWARDED_FOR to get the real IP Address.This might also contain an array of IP addresses when connected through proxies.

Eg : Request.ServerVariables(“HTTP_X_FORWARDED_FOR”);

and check to see if it returns an Empty and then retreive the corresponding IP Address using REMOTE_ADDR.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

One Response

  1. Muthu Vijayan December 19, 2009 Reply

Leave a Reply


x

Interesting BlogPosts to read

Passing Array of Integers to ASP.NET Web API REST Service
You can pass an array of integer to an ASP.NET Web Web API REST service by setting the [FromUri] att...
How to get an Enumerator from range of elements from ArrayList in C# ?
The code snippet shown in this blog post demonstrates how you can get the enumeraor from a range of ...
How to check for the column name in an SqlDataReader Object in C# ?
When working in ADO.NET for data access , there are times when you want to check to see if a column ...