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

Support for proxies and load balancers #66

Merged
merged 2 commits into from
May 29, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions StackExchange.Profiling/IpAddressProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Web;
using System;
using System.Web;

namespace StackExchange.Profiling
{
Expand All @@ -9,10 +10,22 @@ public class IpAddressIdentity : IUserProvider
{
/// <summary>
/// Returns the paramter HttpRequest's client ip address.
/// We combine both the REMOTE_ADDR header (which is the connecting device's IP address),
/// plus the HTTP_X_FORWARDED_FOR header if present (which is set by some proxy
/// servers and load balancers). This allows us to have a unique per-user view, even
/// when behind a proxy or load balancer.
/// </summary>
public string GetUser(HttpRequest request)
{
return request.ServerVariables["REMOTE_ADDR"] ?? "";
// If there's no X_FORWARDED_FOR header, just return REMOTE_ADDR
if (String.IsNullOrWhiteSpace(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
{
return request.ServerVariables["REMOTE_ADDR"] ?? "";
}

// Otherwise return the concatenation of the REMOTE_ADDR and the X_FORWARDED_FOR header
return string.Format("{0} - {1}", request.ServerVariables["REMOTE_ADDR"] ?? "",
request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
}
}
}