Skip to content

Instantly share code, notes, and snippets.

@dcs619
Created January 14, 2015 20:33
Show Gist options
  • Select an option

  • Save dcs619/80befe765b90c6ec82f6 to your computer and use it in GitHub Desktop.

Select an option

Save dcs619/80befe765b90c6ec82f6 to your computer and use it in GitHub Desktop.
IP Address lookup
lblInfo.Text = string.Format( "Device IP: {0} {1} Name: {2}", ipAddress, Environment.NewLine, hostName );
var localaddr = Request.ServerVariables["LOCAL_ADDR"];
lblInfo.Text += "<br> local" + localaddr;
var remoteHost = Request.ServerVariables["REMOTE_HOST"];
lblInfo.Text += "<br> remote" + remoteHost;
var getclient = GetClientIPAddress();
lblInfo.Text += "<br> getclient" + getclient;
var remoteaddr = Request.ServerVariables["REMOTE_ADDR"];
lblInfo.Text += "<br> remote" + remoteaddr;
//(Request.ServerVariables["HTTP_X_FORWARDED_FOR"]) ? Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
//HttpContext.Current.Request.Params["HTTP_CLIENT_IP"] ?? HttpContext.Current.Request.UserHostAddress;
/// <summary>
/// Gets the client ip address.
/// </summary>
/// <returns></returns>
protected string GetClientIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if ( !string.IsNullOrEmpty( ipAddress ) )
{
string[] addresses = ipAddress.Split( ',' );
if ( addresses.Length != 0 )
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment