/// <summary>
        /// 返回验证码json
        /// </summary>
        public string GetVerificationCode()
        {
            Random rd = new Random();

            _PositionX = rd.Next(_MinRangeX, _MaxRangeX);  //设置X坐标
            _PositionY = rd.Next(_MinRangeY, _MaxRangeY);  //设置Y坐标
            string Id = SetSql.Add(_PositionX.ToString()); //X坐标插入数据库(Y坐标给前台)

            int[]   a            = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
            int[]   array        = a.OrderBy(x => Guid.NewGuid()).ToArray();
            Bitmap  bmp          = new Bitmap(path + (new Random()).Next(0, _ImgNum - 1) + ".jpg");
            string  ls_small     = "data:image/jpg;base64," + ImgToBase64String(cutImage(bmp, _shearSize, _shearSize, _PositionX, _PositionY));
            Bitmap  lb_normal    = GetNewBitMap(bmp, _shearSize, _shearSize, _PositionX, _PositionY);
            string  ls_confusion = "data:image/jpg;base64," + ImgToBase64String(ConfusionImage(array, lb_normal));
            JObject jObject      = new JObject();

            jObject["errcode"] = 0;
            jObject["y"]       = _PositionY;              //Y坐标
            jObject["array"]   = string.Join(",", array); //
            jObject["imgx"]    = _ImgWidth;               //图片宽
            jObject["imgy"]    = _ImgHeight;              //图片高
            jObject["small"]   = ls_small;                //裁剪的小图
            jObject["normal"]  = ls_confusion;            //裁剪小图后的原图
            jObject["Id"]      = Id;

            /* errcode: 状态值 成功为0
             * y:裁剪图片y轴位置
             * small:小图字符串
             * normal:剪切小图后的原图并按无序数组重新排列后的图
             * array:无序数组
             * imgx:原图宽
             * imgy:原图高
             */
            return(jObject.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 校验前端是否通过验证
        /// </summary>
        public void CheckCode(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string ls_point = context.Request["point"];           //完成时x轴对于左上角位置位置
            string datelist = context.Request["datelist"];        //滑动过程特征
            string timespan = context.Request["timespan"];        //耗时
            string Id       = context.Request["Id"];              //ID编号,验证X坐标

            string[] result        = SetSql.Seach(Id).Split(','); //获取X坐标和错误次数
            string   code          = result[0];
            string   code_errornum = result[1];
            string   token         = result[2];

            if (code == null)
            {
                WriteError(context, "发生错误"); return;
            }
            if (string.IsNullOrEmpty(ls_point))
            {
                WriteError(context, "未获取到坐标值"); return;
            }
            int li_old_point = 0, li_now_point = 0;

            try { li_old_point = int.Parse(code); }
            catch { WriteError(context, "发生错误-1"); return; }
            try { li_now_point = int.Parse(ls_point); }
            catch { WriteError(context, "获取到的坐标值不正确"); return; }
            //错误
            if (Math.Abs(li_old_point - li_now_point) > _deviationPx)
            {
                int li_count = 0;
                try
                {
                    li_count = int.Parse(code_errornum);
                }
                catch { li_count = 0; }
                SetSql.AddErrorCount(Id);//增加错误次数
                if (li_count > _MaxErrorNum)
                {
                    //超过最大错误次数后不再校验
                    SetSql.Delete(Id, "false");
                    //HttpContext.Current.Session["code"] = null;
                    Write(context, "{\"state\":-1,\"msg\":" + li_count + "}"); return;
                }
                //返回错误次数
                Write(context, "{\"state\":-1,\"msg\":" + li_count + "}"); return;
            }
            if (SlideFeature(datelist))
            {
                //机器人??
                var ji = 2;
            }
            //校验成功 返回正确坐标
            //HttpContext.Current.Session["isCheck"] = "OK";
            //HttpContext.Current.Session["code_errornum"] = null;
            //HttpContext.Current.Session["code"] = null;
            SetSql.Delete(Id, "true");
            Write(context, "{\"state\":0,\"info\":\"正确\",\"data\":" + li_old_point + ",\"token\":\"" + token + "\"}");
        }