File tree Expand file tree Collapse file tree 2 files changed +70
-3
lines changed Expand file tree Collapse file tree 2 files changed +70
-3
lines changed Original file line number Diff line number Diff line change @@ -8,9 +8,10 @@ namespace Ocelot.Security.IPSecurity
88{
99 public class IPSecurityPolicy : ISecurityPolicy
1010 {
11+
1112 public async Task < Response > Security ( DownstreamContext context )
1213 {
13- IPAddress clientIp = context . HttpContext . Connection . RemoteIpAddress ;
14+ var clientIp = context . HttpContext . GetClientIpAddress ( ) ;
1415 SecurityOptions securityOptions = context . DownstreamReRoute . SecurityOptions ;
1516 if ( securityOptions == null )
1617 {
@@ -19,7 +20,7 @@ public async Task<Response> Security(DownstreamContext context)
1920
2021 if ( securityOptions . IPBlockedList != null )
2122 {
22- if ( securityOptions . IPBlockedList . Exists ( f => f == clientIp . ToString ( ) ) )
23+ if ( securityOptions . IPBlockedList . Exists ( f => f == clientIp ) )
2324 {
2425 var error = new UnauthenticatedError ( $ " This request rejects access to { clientIp . ToString ( ) } IP") ;
2526 return new ErrorResponse ( error ) ;
@@ -28,7 +29,7 @@ public async Task<Response> Security(DownstreamContext context)
2829
2930 if ( securityOptions . IPAllowedList != null && securityOptions . IPAllowedList . Count > 0 )
3031 {
31- if ( ! securityOptions . IPAllowedList . Exists ( f => f == clientIp . ToString ( ) ) )
32+ if ( ! securityOptions . IPAllowedList . Exists ( f => f == clientIp ) )
3233 {
3334 var error = new UnauthenticatedError ( $ "{ clientIp . ToString ( ) } does not allow access, the request is invalid") ;
3435 return new ErrorResponse ( error ) ;
Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Http ;
2+ using Microsoft . Extensions . Primitives ;
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+
7+ namespace Ocelot . Security
8+ {
9+ public static class SecurityPolicyExtensions
10+ {
11+ public static string GetClientIpAddress ( this HttpContext httpContext , bool tryUseXForwardHeader = true )
12+ {
13+
14+ string ip = null ;
15+ if ( httpContext == null )
16+ {
17+ return ip ;
18+ }
19+ // X-Forwarded-For => Using the First entry in the list
20+ if ( string . IsNullOrWhiteSpace ( ip ) && tryUseXForwardHeader )
21+ {
22+ ip = httpContext . GetHeaderValue ( "X-Forwarded-For" ) . SplitCsv ( ) . FirstOrDefault ( ) ;
23+ }
24+ // RemoteIpAddress is always null in DNX RC1 Update1 (bug).
25+ if ( string . IsNullOrWhiteSpace ( ip ) && httpContext . Connection ? . RemoteIpAddress != null )
26+ {
27+ ip = httpContext . Connection . RemoteIpAddress . ToString ( ) ;
28+ }
29+ if ( string . IsNullOrWhiteSpace ( ip ) )
30+ {
31+ ip = httpContext . GetHeaderValue ( "REMOTE_ADDR" ) ;
32+ }
33+ if ( ip == "::1" )
34+ {
35+ ip = "127.0.0.1" ;
36+ }
37+ return ip ;
38+ }
39+
40+
41+
42+ public static string GetHeaderValue ( this HttpContext httpContext , string headerName )
43+ {
44+ if ( httpContext ? . Request ? . Headers ? . TryGetValue ( headerName , out StringValues values ) ?? false )
45+ {
46+ return values . ToString ( ) ;
47+ }
48+ return string . Empty ;
49+ }
50+
51+ public static List < string > SplitCsv ( this string csvList , bool nullOrWhitespaceInputReturnsNull = false )
52+ {
53+ if ( string . IsNullOrWhiteSpace ( csvList ) )
54+ {
55+ return nullOrWhitespaceInputReturnsNull ? null : new List < string > ( ) ;
56+ }
57+
58+ return csvList
59+ . TrimEnd ( ',' )
60+ . Split ( ',' )
61+ . AsEnumerable ( )
62+ . Select ( s => s . Trim ( ) )
63+ . ToList ( ) ;
64+ }
65+ }
66+ }
You can’t perform that action at this time.
0 commit comments