Skip to content

Commit

Permalink
Merge pull request #66 from spudstuff/master
Browse files Browse the repository at this point in the history
Support for proxies and load balancers
  • Loading branch information
yellis committed May 29, 2014
2 parents 5f84308 + 64ea0df commit adedd07
Showing 1 changed file with 15 additions and 2 deletions.
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"]);
}
}
}

0 comments on commit adedd07

Please sign in to comment.