This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf0882a
commit 5306bed
Showing
3 changed files
with
33 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
|
||
namespace MvcThrottle.Demo.Helpers | ||
{ | ||
public class NginxIpAddressParser : IpAddressParser | ||
{ | ||
public override string GetClientIp(HttpRequestBase request) | ||
{ | ||
var ipAddress = request.UserHostAddress; | ||
|
||
// get client IP from reverse proxy | ||
var xForwardedFor = request.ServerVariables["HTTP_X_FORWARDED_FOR"]; | ||
if (!string.IsNullOrEmpty(xForwardedFor)) | ||
{ | ||
// Search for public IP addresses | ||
var publicForwardingIps = xForwardedFor.Split(',').Where(ip => !IsPrivateIpAddress(ip)).ToList(); | ||
|
||
// If we found any public IP, return the first one when NGNIX is used, otherwise return the user host address | ||
return publicForwardingIps.Any() ? publicForwardingIps.First().Trim() : ipAddress; | ||
} | ||
|
||
return ipAddress; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters