public static string GetUserIp()
{
string realRemoteIp = string.Empty;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
realRemoteIp =
System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',')[0];
}
if (string.IsNullOrEmpty(realRemoteIp))
{
realRemoteIp = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.IsNullOrEmpty(realRemoteIp))
{
realRemoteIp = System.Web.HttpContext.Current.Request.UserHostAddress;
}
return realRemoteIp;
}
if(Context.Request.ServerVariables["HTTP_VIA"]!=null)
{
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
Request.UserHostAddress
你确定你部署的时候是没有通过反向代理比如nginx或者apache的吗??如果通过这两个,需要在他们上面配置,不然就是127.0.0.1