Example #1
0
        /// <summary>
        /// 根据文字和图片获取验证码图片
        /// </summary>
        /// <param name="content"></param>
        /// <param name="picFileName"></param>
        /// <returns></returns>
        public static VerCodePic GetVerCodePic(string content, string picFileName, int fontSize = 20)
        {
            //ClassLoger.Info("FileHelper.GetVerCodePic", "开始生成二维码");
            //Bitmap bmp = new Bitmap(picFileName);
            //List<int> hlist = new List<int>();
            VerCodePic codepic = new VerCodePic();

            //int i = CreateRandomCode( SetFontPoint.FontPoint.Count - 1);
            //codepic.Font1 = SetFontPoint.FontPoint[i] as FontPoint;
            //hlist.Add(i);

            //A: int i2 = CreateRandomCode( SetFontPoint.FontPoint.Count - 1);
            //if (hlist.Contains(i2))
            //    goto A;
            //codepic.Font2 = SetFontPoint.FontPoint[i2] as FontPoint;
            //hlist.Add(i2);

            //B: int i3 = Utils.GetRandom(0, SetFontPoint.FontPoint.Count - 1);
            //if (hlist.Contains(i3))
            //    goto B;
            //hlist.Add(i3);
            //codepic.Font3 = SetFontPoint.FontPoint[i3] as FontPoint;

            //C: int i4 = Utils.GetRandom(0, SetFontPoint.FontPoint.Count - 1);
            //if (hlist.Contains(i4))
            //    goto C;
            //hlist.Add(i4);
            //codepic.Font4 = SetFontPoint.FontPoint[i4] as FontPoint; string fileName = (content + "-" + picFileName + "-" + i + "|" + i2 + "|" + i3 + "|" + i4).MD5() + Path.GetExtension(picFileName);
            //string dir = Path.Combine(SystemSet.ResourcesPath, SystemSet.VerCodePicPath);
            //string filePath = Path.Combine(dir, fileName);
            //if (File.Exists(filePath))
            //{
            //    codepic.PicURL = string.Format("{0}/{1}/{2}", SystemSet.WebResourcesSite, SystemSet.VerCodePicPath, fileName);
            //    return codepic;
            //}
            //if (!Directory.Exists(dir))
            //{
            //    Directory.CreateDirectory(dir);
            //}

            //Graphics g = Graphics.FromImage(bmp);
            //Font font = new Font("微软雅黑", fontSize, GraphicsUnit.Pixel);
            //SolidBrush sbrush = new SolidBrush(Color.Black);
            //SolidBrush sbrush1 = new SolidBrush(Color.Peru);
            //SolidBrush sbrush2 = new SolidBrush(Color.YellowGreen);
            //SolidBrush sbrush3 = new SolidBrush(Color.SkyBlue);
            //List<char> fontlist = content.ToList();
            //// ClassLoger.Info("FileHelper.GetVerCodePic", fontlist.Count.ToString());
            //g.DrawString(fontlist[0].ToString(), font, sbrush, new PointF(codepic.Font1.X, codepic.Font1.Y));
            //g.DrawString(fontlist[1].ToString(), font, sbrush1, new PointF(codepic.Font2.X, codepic.Font2.Y));
            //g.DrawString(fontlist[2].ToString(), font, sbrush2, new PointF(codepic.Font3.X, codepic.Font3.Y));
            //g.DrawString(fontlist[3].ToString(), font, sbrush3, new PointF(codepic.Font4.X, codepic.Font4.Y));

            //bmp.Save(filePath, ImageFormat.Jpeg);
            //codepic.PicURL = string.Format("{0}/{1}/{2}", SystemSet.WebResourcesSite, SystemSet.VerCodePicPath, fileName);
            return(codepic);
        }
Example #2
0
        /// <summary>
        /// 获取验证码,有效时间10分钟
        /// </summary>
        /// <returns></returns>
        public JsonResult VerCodeImg()
        {
            JsonResult <VerCodePicViewModel> result = new JsonResult <VerCodePicViewModel>();

            result.code = 1;
            result.msg  = "OK";
            try
            {
                //生成随机的中文验证码
                string yzm = "人口手大小多少上中下男女天地会反清复明杨中科小宝双儿命名空间语现在明天来多个的我山东河北南固安北京南昌东海西安是沙河高教园学"
                             + "木禾上下土个八入大天人火文六七儿九无口日中了子门月不开四五目耳头米见白田电也长山出飞马鸟云公车牛羊小少巾牙尺毛又心手水广升足"
                             + "走方半巴业本平书自已东西回片皮生里果几用鱼今正雨两瓜衣来年左右万百丁齐冬说友话春朋高你绿们花红草爷亲节的岁行古处声知多忙洗真认父扫"
                             + "母爸写全完关家看笑着兴画会妈合奶放午收女气太早去亮和李语秀千香听远唱定连向以更后意主总先起干明赶净同专工才级队蚂蚁前房空网诗黄林闭"
                             + "童立是我朵叶美机她过他时送让吗往吧得虫很河借姐呢呀哪谁凉怕量跟最园脸因阳为光可法石找办许别那到都吓叫再做象点像照沙海桥军竹苗井面乡"
                             + "忘想念王这从进边道贝男原爱虾跑吹乐地老快师短淡对热冷情拉活把种给吃练学习非苦常问伴间共伙汽分要没孩位选北湖南秋江只帮星请雪就球跳玩"
                             + "桃树刚兰座各带坐急名发成动晚新有么在变什条";
                Random r         = new Random();
                string validCode = "";
                for (int i = 0; i < 4; i++)
                {
                    int number = r.Next(0, yzm.Length);
                    validCode += yzm[number];
                }


                VerCodePicViewModel vcvm = new VerCodePicViewModel();

                string sourcePic = GetVerCodePicResource();
                if (sourcePic.IsNull() || !System.IO.File.Exists(sourcePic))
                {
                    sourcePic = @"D:\zm\xunguang-4.jpg";
                }
                VerCodePic codepic = GetVerCodePic(validCode, sourcePic);
                vcvm.content  = validCode;
                vcvm.MainPic  = codepic.PicURL;
                result.Result = vcvm;

                PublicConfig.SetCookie("ValidateCode", validCode, 0, 30, true);
                string token = codepic.Font1.X + "," + codepic.Font1.Y + "," + codepic.Font2.X + "," + codepic.Font2.Y + ","
                               + codepic.Font3.X + "," + codepic.Font3.Y + "," + codepic.Font4.X + "," + codepic.Font4.Y;
                PublicConfig.SetCookie("__RequestNcoSig", token, 0, 30, true);
                Session["codepic"]  = (codepic.Font1.X + "," + codepic.Font1.Y + "," + codepic.Font2.X + "," + codepic.Font2.Y + "," + codepic.Font3.X + "," + codepic.Font3.Y + "," + codepic.Font4.X + "," + codepic.Font4.Y);
                Session["fontSize"] = 20;

                result.ResultMsg = "";
            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg  = "AccountController.VerCodePic发生异常:" + ex.Message;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public async Task <IHttpActionResult> VerCodePic()
        {
            JsonResult <VerCodePicViewModel> result = new JsonResult <VerCodePicViewModel>();

            result.code = 1;
            result.msg  = "OK";
            await Task.Run(() => {
                try
                {
                    ClassLoger.Info("VerCodePic", "开始获取成语");
                    cy_dictBll cybll       = new cy_dictBll();
                    IList <cy_dict> cylist = cybll.GetAllcy_dict();
                    ClassLoger.Info("VerCodePic", cylist.Count.ToString());
                    int i = Utils.GetRandom(0, cylist.Count - 1);
                    ClassLoger.Info("VerCodePic", i.ToString());
                    cy_dict cy = cylist[i];
                    ClassLoger.Info("VerCodePic成语:", cy.chengyu);
                    VerCodePicViewModel vcvm = new VerCodePicViewModel();

                    string sourcePic = FileHelper.GetVerCodePicResource();
                    if (sourcePic.IsNull() || !File.Exists(sourcePic))
                    {
                        sourcePic = @"E:\WebResources\images\VerCodePicSource\1.jpg";
                    }
                    ClassLoger.Info("VerCodePic图片", sourcePic);
                    VerCodePic codepic = FileHelper.GetVerCodePic(cy.chengyu, sourcePic);
                    vcvm.content       = cy.chengyu;
                    vcvm.MainPic       = codepic.PicURL;
                    result.Result      = vcvm;

                    string key = cookieKey();
                    RedisBase.Item_Set(key, codepic);
                    RedisBase.ExpireEntryAt(key, DateTime.Now.AddMinutes(10));
                    result.ResultMsg = key;
                }
                catch (Exception ex)
                {
                    ClassLoger.Error("AccountController.VerCodePic", ex);
                    result.code = -1;
                    result.msg  = "AccountController.VerCodePic发生异常:" + ex.Message;
                }
            });

            return(Ok(result));
        }
        public async Task <IHttpActionResult> CheckPicCode([FromBody] CheckPicCodeViewModel piccode)
        {
            JsonResult <bool> result = new JsonResult <bool>();

            result.code = 1;
            result.msg  = "OK";
            if (piccode == null)
            {
                result.Result    = false;
                result.ResultMsg = "参数错误";
                return(Ok(result));
            }

            if (string.IsNullOrEmpty(piccode.token) || !RedisBase.ContainsKey(piccode.token))
            {
                result.Result    = false;
                result.ResultMsg = "验证码已过期";
                return(Ok(result));
            }
            result.Result = await Task.Run <bool>(() => {
                bool flag          = false;
                VerCodePic codepic = RedisBase.Item_Get <VerCodePic>(piccode.token);
                if (Math.Abs(codepic.Font1.X - piccode.x1) > 0.5 || Math.Abs(codepic.Font1.Y - piccode.y1) > 0.5 ||
                    Math.Abs(codepic.Font2.X - piccode.x2) > 0.5 || Math.Abs(codepic.Font2.Y - piccode.y2) > 0.5 ||
                    Math.Abs(codepic.Font3.X - piccode.x3) > 0.5 || Math.Abs(codepic.Font3.Y - piccode.y3) > 0.5 ||
                    Math.Abs(codepic.Font4.X - piccode.x4) > 0.5 || Math.Abs(codepic.Font4.Y - piccode.y4) > 0.5)
                {
                    flag             = false;
                    result.ResultMsg = "验证码错误";
                }
                else
                {
                    flag             = true;
                    result.ResultMsg = "验证码正确";
                }
                return(flag);
            });

            return(Ok(result));
        }
        public async Task <IHttpActionResult> SendSmSCode([FromBody] SendSMSCodeViewModel sendSmsCode)
        {
            JsonResult <string> result = new JsonResult <string>();

            result.code = 1;
            result.msg  = "OK";
            if (sendSmsCode == null)
            {
                result.code = 0;
                result.msg  = "参数错误";
                return(Ok(result));
            }
            string requestTimeskey = "AccountController.SendSmSCode";
            double times           = RedisBase.SortedSet_ZSCORE <string>(requestTimeskey, sendSmsCode.token);

            //if (times > 0)
            //{
            //    result.code = 0;
            //    result.msg = "验证码已失效";
            //    return Ok(result);
            //}

            if (string.IsNullOrEmpty(sendSmsCode.token) || !RedisBase.ContainsKey(sendSmsCode.token))
            {
                result.code = 0;
                result.msg  = "验证码已过期";
                return(Ok(result));
            }
            if (sendSmsCode.type == 0 && !sendSmsCode.tel.IsMobile())
            {
                result.code = 0;
                result.msg  = "invalid tel";
                return(Ok(result));
            }
            await Task.Run(() => {
                RedisBase.SortedSet_Zincrby <string>(requestTimeskey, sendSmsCode.token, 1);
                RedisBase.SortedSet_SetExpire(requestTimeskey, DateTime.Now.AddMinutes(10));

                VerCodePic codepic = RedisBase.Item_Get <VerCodePic>(sendSmsCode.token);
                if (Math.Abs(codepic.Font1.X - sendSmsCode.x1) > 0.5 || Math.Abs(codepic.Font1.Y - sendSmsCode.y1) > 0.5 ||
                    Math.Abs(codepic.Font2.X - sendSmsCode.x2) > 0.5 || Math.Abs(codepic.Font2.Y - sendSmsCode.y2) > 0.5 ||
                    Math.Abs(codepic.Font3.X - sendSmsCode.x3) > 0.5 || Math.Abs(codepic.Font3.Y - sendSmsCode.y3) > 0.5 ||
                    Math.Abs(codepic.Font4.X - sendSmsCode.x4) > 0.5 || Math.Abs(codepic.Font4.Y - sendSmsCode.y4) > 0.5)
                {
                    result.code = 0;
                    result.msg  = "验证码错误";
                }
                else
                {
                    //产生短信验证码
                    int code   = Utils.GetRandom(100000, 999999);
                    string key = cookieKey();
                    RedisBase.Item_Set(key, code.TryToString());
                    RedisBase.ExpireEntryAt(key, DateTime.Now.AddMinutes(10));
                    string tel = sendSmsCode.tel;
                    if (sendSmsCode.type == 1)
                    {
                        tel = RedisBase.Item_Get <string>(sendSmsCode.tel);
                    }
                    bool isSend = Utils.SendSMSVCode(code.TryToString(), tel);
                    if (isSend)
                    {
                        result.Result = key;
                    }
                    else
                    {
                        result.code = 0;
                        result.msg  = "发送失败";
                    }
                }
            });

            return(Ok(result));
        }
Example #6
0
        /// <summary>
        /// 根据文字和图片获取验证码图片
        /// </summary>
        /// <param name="content"></param>
        /// <param name="picFileName"></param>
        /// <returns></returns>
        public static VerCodePic GetVerCodePic(string content, string picFileName, int fontSize = 20)
        {
            Bitmap     bmp     = new Bitmap(picFileName);
            List <int> hlist   = new List <int>();
            VerCodePic codepic = new VerCodePic();
            int        i       = GetRandom(0, FontPoints.Count - 1);

            codepic.Font1 = FontPoints[i] as FontPoint;
            hlist.Add(i);

            A : int i2 = GetRandom(0, FontPoints.Count - 1);
            if (hlist.Contains(i2))
            {
                goto A;
            }
            codepic.Font2 = FontPoints[i2] as FontPoint;
            hlist.Add(i2);

            B : int i3 = GetRandom(0, FontPoints.Count - 1);
            if (hlist.Contains(i3))
            {
                goto B;
            }
            hlist.Add(i3);
            codepic.Font3 = FontPoints[i3] as FontPoint;

            C : int i4 = GetRandom(0, FontPoints.Count - 1);
            if (hlist.Contains(i4))
            {
                goto C;
            }
            hlist.Add(i4);
            codepic.Font4 = FontPoints[i4] as FontPoint;

            PicPoints = codepic;

            string fileName = (content + "-" + picFileName + "-" + i + "|" + i2 + "|" + i3 + "|" + i4).ToEncrypt() + Path.GetExtension(picFileName);
            string dir      = Path.Combine("D:/ZM/ZMMVC/WAPUI/", "updata/");
            string filePath = Path.Combine(dir, fileName);

            if (System.IO.File.Exists(filePath))
            {
                codepic.PicURL = string.Format("{0}/{1}/{2}", "http://localhost:55292", "updata", fileName);
                return(codepic);
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            Graphics    g        = Graphics.FromImage(bmp);
            Font        font     = new Font("微软雅黑", fontSize, GraphicsUnit.Pixel);
            SolidBrush  sbrush   = new SolidBrush(Color.White);
            SolidBrush  sbrush1  = new SolidBrush(Color.White);
            SolidBrush  sbrush2  = new SolidBrush(Color.YellowGreen);
            SolidBrush  sbrush3  = new SolidBrush(Color.SkyBlue);
            List <char> fontlist = content.ToList();

            g.DrawString(fontlist[0].ToString(), font, sbrush, new PointF(codepic.Font1.X, codepic.Font1.Y));
            g.DrawString(fontlist[1].ToString(), font, sbrush1, new PointF(codepic.Font2.X, codepic.Font2.Y));
            g.DrawString(fontlist[2].ToString(), font, sbrush2, new PointF(codepic.Font3.X, codepic.Font3.Y));
            g.DrawString(fontlist[3].ToString(), font, sbrush3, new PointF(codepic.Font4.X, codepic.Font4.Y));

            bmp.Save(filePath, ImageFormat.Jpeg);
            codepic.PicURL = string.Format("{0}/{1}/{2}", "http://localhost:55292", "updata", fileName);
            return(codepic);
        }