/// <summary>
/// 获取远程访问用户的Ip地址
/// </summary>
/// <returns>返回Ip地址</returns>
protected string GetLoginIp()
{
string loginip = "";
//Request.ServerVariables[""]--获取服务变量集合
if (Request.ServerVariables["REMOTE_ADDR"] != null) //判断发出请求的远程主机的ip地址是否为空
{
//获取发出请求的远程主机的Ip地址
loginip = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
//判断登记用户是否使用设置代理
else if (Request.ServerVariables["HTTP_VIA"] != null)
{
if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
//获取代理的服务器Ip地址
loginip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
//获取客户端IP
loginip = Request.UserHostAddress;
}
}
else
{
//获取客户端IP
loginip = Request.UserHostAddress;
}
return loginip;
}
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else// not using proxy or can‘t get the Client IP
{
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can‘t get the Client IP, it will return proxy IP.
}