Showing posts with label .NET. Show all posts
Showing posts with label .NET. Show all posts

Monday, October 6, 2008

Getting Client's IP in ASP.NET

ASP .Net web page:
  1. Dim strClientIP As String
  2. strClientIP = Request.UserHostAddress()
  3. Response.Write(strClientIP)
ASP .NET web services:

Context.Request.ServerVariables ["REMOTE_ADDR"]

OR

string ip;

ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if(ip==string.Empty)
{
ip=Request.ServerVariables("REMOTE_ADDR");
}

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.