Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with User IP and X-Forwarded-By #46

Open
olaj opened this issue Feb 23, 2016 · 2 comments
Open

Issue with User IP and X-Forwarded-By #46

olaj opened this issue Feb 23, 2016 · 2 comments
Labels

Comments

@olaj
Copy link

olaj commented Feb 23, 2016

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.

@AlbertoMonteiro
Copy link
Collaborator

@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 😛

@balexandre
Copy link

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants