1
如何提取ASP.NET 5/MVC 6 vNext中的IPAddress? 我正在使用RC1。IPAddress null for MVC 6 ASP.NET 5
這不起作用,並且在非本地主機地址上返回null。
if (httpContext.Connection.RemoteIpAddress != null)
{
currentIp = httpContext.Connection.RemoteIpAddress.ToString();
}
這工作,但它是凌亂:
private static string GetIPAddress(HttpRequest httpRequest)
{
string keys = string.Empty;
foreach (string key in httpRequest.Headers.Keys)
{
keys = keys + key + "#";
}
string[] arrKeys = keys.Split('#');
int x = 0;
foreach (string value in httpRequest.Headers.Values)
{
if (arrKeys[x].Trim() == "X-Forwarded-For")
{
string[] arrIP = value.Split(':');
if (arrIP[0] != null)
{
// ip found
return arrIP[0];
}
}
x++;
}
// no ip found
return string.Empty;
}