获取来访IP和来访地址

注册的时候需要保存用户的ip和地址,就直接拿来用了。

region 获取来访地址和IP

 /// <summary>
        /// 获取来访地址和IP
        /// </summary>
        /// <returns>来访地址,来访IP</returns>
        public static List<string> GetFromSiteAndIP()
        {
            List<string> LstSiteAndIP = new List<string>();
            try
            {
                string strFromSiteName = string.Empty;

                string strUserIP = string.Empty;

                if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_REFERER"]))
                {
                    string strFromUrl = HttpContext.Current.Request.ServerVariables["HTTP_REFERER"].ToString();

                    XmlNodeList nodes = GetXMLNodes("Token");
                    foreach (XmlNode node in nodes)
                    {
                        if (strFromUrl.IndexOf(node.Attributes["site"].Value) >= 0)
                        {
                            strFromSiteName = node.Attributes["site"].Value;
                            break;
                        }
                    }
                }

                if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) //得到穿过代理服务器的ip地址
                {
                    strUserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else
                {
                    strUserIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
                }

                LstSiteAndIP.Add(strFromSiteName);
                LstSiteAndIP.Add(strUserIP);
            }
            catch (Exception ex)
            {
                clsWriteLog.WriteLogText("获取来访IP和地址错误",ex.ToString());
            }

            return LstSiteAndIP;
        }
        #endregion

你可能感兴趣的:(注册ip,注册地址)