Issue with User IP and X-Forwarded-By
Not sure how to fix this but this method.
private static string IpFromXForwardedFor(HttpRequest request)
{
var forwardedFor = request.Headers["X-Forwarded-For"];
if (!string.IsNullOrEmpty(forwardedFor) && forwardedFor.Contains(","))
{
forwardedFor = forwardedFor.Split(',').Last().Trim();
}
return forwardedFor;
}
Didn't work well with my setup with Cloudflare. My header was like this.
X_FORWARDED_FOR: 196.215.208.37, 197.214.212.59:57956
Where the first one is the "good ip" and the second one is from Cloudflare. RollbarSharp was taking the last one and Rollbar didn't even accept that as an IP (because of the port i guess) and well it's the Cloudflare IP anyway so not that interesting. Just wanted to let you know. I run with a fork now where i take the first IP instead.
@olaj I use AWS, and AWS does use this pattern
X-Forwarded-For: OriginatingClientIPAddress, proxy1-IPAddress, proxy2-IPAddress
Source: X-Forwarded Headers for Elastic Load Balancing
In this case I guess that cloudflare use the same pattern, so we could use the first IP address instead the last one.
Could you fix that?
If yes, doesn't forget the unit tests :stuck_out_tongue:
actually CloudFlare passes the correct user IP Address in it's own header.
so, just to include CF the code should also have:
var ipCloudFlare = request.Headers["CF-Connecting-IP"];
if (!string.IsNullOrEmpty(ipCloudFlare))
return ipCloudFlare;