Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ipList"></param>
        /// <returns></returns>
        public static string EncodeLockIP(string ipList)
        {
            StringBuilder builder = new StringBuilder(0x100);

            if (!string.IsNullOrEmpty(ipList.Trim()))
            {
                string[] strArray = ipList.Split(new char[] { '\n' });
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (!string.IsNullOrEmpty(strArray[i]) && strArray[i].Contains("----"))
                    {
                        string[] strArray2 = strArray[i].Split(new string[] { "----" }, StringSplitOptions.RemoveEmptyEntries);
                        if (strArray2.Length < 2)
                        {
                            throw new ArgumentException("请填写正确网站黑白名单中的IP地址!");
                        }
                        if (!JosonValidate.IsIP(strArray2[0]) || !JosonValidate.IsIP(strArray2[1]))
                        {
                            throw new ArgumentException("请填写正确网站黑白名单中的IP地址!");
                        }
                        if (i == 0)
                        {
                            builder.Append(EncodeIP(strArray2[0]) + "----" + EncodeIP(strArray2[1]));
                        }
                        else
                        {
                            builder.Append(string.Concat(new object[] { "$$$", EncodeIP(strArray2[0]), "----", EncodeIP(strArray2[1]) }));
                        }
                    }
                }
            }
            return(builder.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取客户端IP
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            string userHostAddress = string.Empty;

            userHostAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.UserHostAddress;
            }
            if (!(!string.IsNullOrEmpty(userHostAddress) && JosonValidate.IsIP(userHostAddress)))
            {
                return("127.0.0.1");
            }
            return(userHostAddress);
        }