コード例 #1
0
        /// <summary>
        /// 验证是否是合法的请求标识
        /// </summary>
        /// <returns></returns>
        public static bool CheckIsValidRequestSign(string sign)
        {
            bool result = false;

            if (string.IsNullOrEmpty(sign))
            {
                return(false);
            }
            try
            {
                //1 先解密
                string deCodeString = LZString.Decompress(sign, true);
                //2 获取里面的时间戳内容对应的时间
                DateTime clientTimeSign = deCodeString.ToLong().ConvertUnixTimeTokenToDateTime();
                //3 比较时间是否超出 2分钟 超过2分钟的请求sign 标志为失效的请求
                if (DateTime.Now.Subtract(clientTimeSign).TotalSeconds <= SignTimeOut)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

            return(result);
        }
コード例 #2
0
 public void Decompress(LZStringTestCase test)
 {
     Assert.That(LZString.Decompress(test.Compressed), Is.EqualTo(test.Uncompressed));
 }
コード例 #3
0
            public static String DecompressFromUTF16(String input)
            {
                if (input == null)
                {
                    return("");
                }
                StringBuilder output = new StringBuilder(200);
                int           current = 0, c, status = 0, i = 0;

                while (i < input.Length)
                {
                    c = (((int)input[i]) - 32);

                    switch (status++)
                    {
                    case 0:
                        current = c << 1;
                        break;

                    case 1:
                        output.Append((char)(current | (c >> 14)));
                        current = (c & 16383) << 2;
                        break;

                    case 2:
                        output.Append((char)(current | (c >> 13)));
                        current = (c & 8191) << 3;
                        break;

                    case 3:
                        output.Append((char)(current | (c >> 12)));
                        current = (c & 4095) << 4;
                        break;

                    case 4:
                        output.Append((char)(current | (c >> 11)));
                        current = (c & 2047) << 5;
                        break;

                    case 5:
                        output.Append((char)(current | (c >> 10)));
                        current = (c & 1023) << 6;
                        break;

                    case 6:
                        output.Append((char)(current | (c >> 9)));
                        current = (c & 511) << 7;
                        break;

                    case 7:
                        output.Append((char)(current | (c >> 8)));
                        current = (c & 255) << 8;
                        break;

                    case 8:
                        output.Append((char)(current | (c >> 7)));
                        current = (c & 127) << 9;
                        break;

                    case 9:
                        output.Append((char)(current | (c >> 6)));
                        current = (c & 63) << 10;
                        break;

                    case 10:
                        output.Append((char)(current | (c >> 5)));
                        current = (c & 31) << 11;
                        break;

                    case 11:
                        output.Append((char)(current | (c >> 4)));
                        current = (c & 15) << 12;
                        break;

                    case 12:
                        output.Append((char)(current | (c >> 3)));
                        current = (c & 7) << 13;
                        break;

                    case 13:
                        output.Append((char)(current | (c >> 2)));
                        current = (c & 3) << 14;
                        break;

                    case 14:
                        output.Append((char)(current | (c >> 1)));
                        current = (c & 1) << 15;
                        break;

                    case 15:
                        output.Append((char)(current | c));

                        status = 0;
                        break;
                    }

                    i++;
                }

                return(LZString.Decompress(output.ToString()));
                // return output;
            }
コード例 #4
0
        /// <summary>
        /// 监测是否来自浏览器
        /// </summary>
        /// <param name="actionContext"></param>
        private bool CheckIsComeFromWebBrowser(ActionExecutingContext actionContext)
        {
            bool   result    = false;
            string userAgent = actionContext.HttpContext.Request.Headers[HttpServerProxy.RequestHeaderKeyUserAgent];

            //验证UA
            if (string.IsNullOrEmpty(userAgent))
            {
                return(false);
            }

            //验证请求参数sign
            string requestSign = actionContext.HttpContext.Request.GetQuery <string>("sign");

            if (string.IsNullOrEmpty(userAgent))
            {
                return(false);
            }
            else
            {
                //验证sign 标识
                var isValidSign = WorkContext.CheckIsValidRequestSign(requestSign);
                if (false == isValidSign)
                {
                    return(false);
                }
            }

            //验证cookie 标识
            var cookieWebbrowserSign = actionContext.HttpContext.GetCookie <string>(Contanst.Cookie_Key_BrowserSign);

            if (string.IsNullOrEmpty(cookieWebbrowserSign))
            {
                return(false);
            }

            try
            {
                //这是加密内容cookieWebbrowserSingn = string.Concat(WorkContext.SiteName, ":", DateTime.Now.ToString());
                //解密cookie
                string signText = LZString.Decompress(cookieWebbrowserSign, true);

                if (!string.IsNullOrEmpty(signText))
                {
                    if (signText.Contains(WorkContext.SiteName))
                    {
                        string time = signText.Split('|')[1];//获取里面的时间 超过3小时 必须刷新页面 否则过期
                        if (!string.IsNullOrEmpty(time) && DateTime.Now.Subtract(time.ToDatetime()).Hours < 3)
                        {
                            result = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }


            return(result);
        }