Skip to content

Instantly share code, notes, and snippets.

@ylv-io
Created April 24, 2018 20:08
Show Gist options
  • Save ylv-io/cc3064589efbfdff7a60674a06f74566 to your computer and use it in GitHub Desktop.
Save ylv-io/cc3064589efbfdff7a60674a06f74566 to your computer and use it in GitHub Desktop.
Detects client's IP Address Behind Cloudflare using ASP.NET MVC
public static class RequestExtensions
{
public static string GetIpAddress(this HttpRequestBase request)
{
if (request.Headers["CF-CONNECTING-IP"] != null)
return request.Headers["CF-CONNECTING-IP"];
var ipAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
var addresses = ipAddress.Split(',');
if (addresses.Length != 0)
return addresses[0];
}
return request.UserHostAddress;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment