Skip to content

Commit 1ee91fb

Browse files
committed
WebHelper.GetCurrentIPAddress() maps IPv6 to IPv4
1 parent 8755e12 commit 1ee91fb

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

src/Libraries/SmartStore.Core/WebHelper.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)