File tree Expand file tree Collapse file tree
src/Libraries/SmartStore.Core Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -91,29 +91,43 @@ public virtual string GetCurrentIpAddress()
9191 } ;
9292
9393 string result = null ;
94+ IPAddress ipv6 = null ;
9495
9596 foreach ( var key in keysToCheck )
9697 {
9798 var ipString = vars [ key ] ;
98- if ( ipString . HasValue ( ) )
99+
100+ if ( ! string . IsNullOrEmpty ( ipString ) )
99101 {
100102 var arrStrings = ipString . Split ( ',' ) ;
101- // Take the last entry
102- ipString = arrStrings [ arrStrings . Length - 1 ] . Trim ( ) ;
103103
104- IPAddress address ;
105- if ( IPAddress . TryParse ( ipString , out address ) )
106- {
107- result = ipString ;
108- break ;
109- }
104+ // Iterate list from end to start (IPv6 addresses usually have precedence)
105+ for ( int i = arrStrings . Length - 1 ; i >= 0 ; i -- )
106+ {
107+ ipString = arrStrings [ i ] . Trim ( ) ;
108+
109+ if ( IPAddress . TryParse ( ipString , out var address ) )
110+ {
111+ if ( address . AddressFamily == AddressFamily . InterNetworkV6 )
112+ {
113+ ipv6 = address ;
114+ }
115+ else
116+ {
117+ result = ipString ;
118+ break ;
119+ }
120+ }
121+ }
110122 }
111123 }
112124
113- if ( result == "::1" )
114- {
115- result = "127.0.0.1" ;
116- }
125+ if ( string . IsNullOrEmpty ( result ) && ipv6 != null )
126+ {
127+ result = ipv6 . ToString ( ) == "::1"
128+ ? "127.0.0.1"
129+ : ipv6 . MapToIPv4 ( ) . ToString ( ) ;
130+ }
117131
118132 return ( _ipAddress = result . EmptyNull ( ) ) ;
119133 }
You can’t perform that action at this time.
0 commit comments