我正在尝试获取远程IP地址
On Startup.cs
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
然后我使用Request.HttpContext.Connection.RemoteIpAddress.ToString()
来检索IP。我发布到主机,它返回错误的IP,在香港的某个地方,而不是我在其他亚洲国家的真实IP。我在不同位置查了几台电脑,都显示错误的IP。
答案 0 :(得分:-1)
你应该试试这个
protected string GetIPAddress()
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!string.IsNullOrEmpty(ipAddress))
{
string[] addresses = ipAddress.Split(',');
if (addresses.Length != 0)
{
return addresses[0];
}
}
return context.Request.ServerVariables["REMOTE_ADDR"];
}