62,041
社区成员
发帖
与我相关
我的任务
分享
public static string IPAddressAll
{
get
{
string ip = HttpContext.Current.Request.UserHostAddress;
string agentip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if(!string.IsNullOrEmpty(agentip))
{
//有代理
if(agentip.IndexOf(".")==-1)
agentip = null;
if(agentip != null)
{
if(agentip.IndexOf("unknow")!=-1)
agentip = agentip.Replace("unknow", string.Empty);
string[] temparyip = agentip.Replace(" ", string.Empty).Replace("'", string.Empty).Split(new char[] { ',', ';' });
//过滤代理格式中的非IP和内网IP
for (int i = 0; i < temparyip.Length; i++)
{
if (temparyip != string.Empty
&& IsIPAddress(temparyip)
&& temparyip.Substring(0, 3) != "10."
&& temparyip.Substring(0, 7) != "192.168"
&& temparyip.Substring(0, 7) != "172.16.")
{
ip += "," + temparyip;
}
}
}
}
else
{
agentip = null;
}
return ip;
}
}