获取当前访问的客户端IP地址 IPV4 与IPV6
string VisitorIP = ""; string VisitorIPV6 = ""; var request = filterContext.RequestContext.HttpContext.Request; if (request.ServerVariables["HTTP_VIA"] != null) // 服务器, using proxy { //得到真实的客户端地址 VisitorIP = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP. } else//如果没有使用代理服务器或者得不到客户端的ip not using proxy or can't get the Client IP { // 得到服务端的地址 var ip = request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP. if (ip.Equals("::1"))//::-1是ipV6地址,实际上就是本机的意思 { foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { //InterNetwork ipv4地址 if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { VisitorIP = _IPAddress.ToString(); } //InterNetworkV6 ipv6地址 if (_IPAddress.AddressFamily.ToString() == "InterNetworkV6") { VisitorIPV6 = _IPAddress.ToString(); } } }