Cloud Training (Online)

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;



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.

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.