国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

Asp.net Web Api實現圖片點擊式圖片驗證碼功能

來源:懂視網 責編:小采 時間:2020-11-27 22:35:32
文檔

Asp.net Web Api實現圖片點擊式圖片驗證碼功能

Asp.net Web Api實現圖片點擊式圖片驗證碼功能:現在驗證碼的形式越來越豐富,今天要實現的是在點擊圖片中的文字來進行校驗的驗證碼,如圖 這種驗證碼驗證是驗證鼠標是否選中了圖片中文字的位置,以及選擇的順序,產生驗證碼的時候可以提供一組底圖,然后隨機獲取一張圖片,隨機選取幾個字,然后把文字的順
推薦度:
導讀Asp.net Web Api實現圖片點擊式圖片驗證碼功能:現在驗證碼的形式越來越豐富,今天要實現的是在點擊圖片中的文字來進行校驗的驗證碼,如圖 這種驗證碼驗證是驗證鼠標是否選中了圖片中文字的位置,以及選擇的順序,產生驗證碼的時候可以提供一組底圖,然后隨機獲取一張圖片,隨機選取幾個字,然后把文字的順

現在驗證碼的形式越來越豐富,今天要實現的是在點擊圖片中的文字來進行校驗的驗證碼,如圖

這種驗證碼驗證是驗證鼠標是否選中了圖片中文字的位置,以及選擇的順序,產生驗證碼的時候可以提供一組底圖,然后隨機獲取一張圖片,隨機選取幾個字,然后把文字的順序打亂,分別隨機放到圖片的一個位置上,然后記錄文字的位置和順序,驗證的時候驗證一下文字的位置和順序即可

驗證碼圖片的類

/// <summary>
 /// 二維碼圖片
 /// </summary>
 public class VerCodePic
 {
 /// <summary>
 /// 圖片鏈接
 /// </summary>
 public string PicURL { get; set; }
 /// <summary>
 /// 第一個字位置
 /// </summary>
 public FontPoint Font1 { get; set; }
 /// <summary>
 /// 第二個字位置
 /// </summary>
 public FontPoint Font2 { get; set; }
 /// <summary>
 /// 第三個字位置
 /// </summary>
 public FontPoint Font3 { get; set; }
 /// <summary>
 /// 第四個字位置
 /// </summary>
 public FontPoint Font4 { get; set; }
 }
 /// <summary>
 /// 文字位置
 /// </summary>
 public class FontPoint
 {
 public int X { get; set; }
 public int Y { get; set; }
 }

生成驗證碼圖片驗證碼的方法,在這個方法中指定了生成的驗證碼圖片中字體大小為20個像素,因為驗證碼底圖的大小是固定的,所以就把驗證碼底圖按照字體的大小分成了若干個網格位置,指定一個文字在圖片中的位置時只需要隨機獲取其中一個網格即可,如果這個網格中沒有指定過文字,那就把文字放到這個網格中。

提前設定網格的方法

 private static ArrayList _FontPoint;
 public static ArrayList FontPoint
 {
 get
 {
 if (_FontPoint==null)
 {
 _FontPoint = new ArrayList();
 for (int x=0;x<10;x++)
 {
 for (int y=0;y<5;y++)
 {
 _FontPoint.Add(new Models.FontPoint() { X = x * 28, Y = y * 20 });
 }
 }
 }
 return _FontPoint;
 }
 }

我選定的驗證碼底圖為280*100的,所以按照上邊的方法將圖片分成了若干個網格,在下邊設定一個文字位置的時候隨機選取其中一個位置,而且給每個字都設定了不一樣的顏色

/// <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 = Utils.GetRandom(0, SystemSet.FontPoint.Count - 1);
 codepic.Font1 = SystemSet.FontPoint[i] as FontPoint;
 hlist.Add(i);
 A: int i2 = Utils.GetRandom(0, SystemSet.FontPoint.Count - 1);
 if (hlist.Contains(i2))
 goto A;
 codepic.Font2 = SystemSet.FontPoint[i2] as FontPoint;
 hlist.Add(i2);
 B: int i3 = Utils.GetRandom(0, SystemSet.FontPoint.Count - 1);
 if (hlist.Contains(i3))
 goto B;
 hlist.Add(i3);
 codepic.Font3 = SystemSet.FontPoint[i3] as FontPoint;
 C: int i4 = Utils.GetRandom(0, SystemSet.FontPoint.Count - 1);
 if (hlist.Contains(i4))
 goto C;
 hlist.Add(i4);
 codepic.Font4 = SystemSet.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].TryToString(), font, sbrush, new PointF(codepic.Font1.X, codepic.Font1.Y));
 g.DrawString(fontlist[1].TryToString(), font, sbrush1, new PointF(codepic.Font2.X, codepic.Font2.Y));
 g.DrawString(fontlist[2].TryToString(), font, sbrush2, new PointF(codepic.Font3.X, codepic.Font3.Y));
 g.DrawString(fontlist[3].TryToString(), 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;
 }

獲取圖片驗證碼的api接口,在這個接口中從成語庫中隨機選取了一個成語,然后隨機選取了一個圖片,然后調用生成圖片驗證碼的方法,生成了圖片驗證碼,并且把驗證碼對應的信息緩存在redis中,設定緩存時間,將redis的key作為一個臨時令牌隨同驗證碼返回

/// <summary>
 /// 獲取驗證碼,有效時間10分鐘
 /// </summary>
 /// <returns></returns>
 [HttpGet]
 [Route("vercode")]
 public JsonResult<VerCodePicViewModel> VerCodePic()
 {
 JsonResult<VerCodePicViewModel> result = new JsonResult<VerCodePicViewModel>();
 result.code = 1;
 result.msg = "OK";
 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 result;
 }

效果如圖:

圖片驗證碼校驗接口參數結構

public class CheckPicCodeViewModel
 {
 /// <summary>
 /// 客戶端令牌
 /// </summary>
 public string token { get; set; }
 public double x1 { get; set; }
 public double x2 { get; set; }
 public double x3 { get; set; }
 public double x4 { get; set; }
 public double y1 { get; set; }
 public double y2 { get; set; }
 public double y3 { get; set; }
 public double y4 { get; set; }
 }

驗證碼校驗接口

/// <summary>
 /// 校驗圖片驗證碼是否正確
 /// </summary>
 /// <param name="piccode"></param>
 /// <returns></returns>
 [HttpPost]
 [Route("checkpiccode")]
 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);
 }

傳入用戶選中的位置和順序,并對其進行驗證。

以上所述是小編給大家介紹的Asp.net Web Api實現圖片點擊式圖片驗證碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Asp.net Web Api實現圖片點擊式圖片驗證碼功能

Asp.net Web Api實現圖片點擊式圖片驗證碼功能:現在驗證碼的形式越來越豐富,今天要實現的是在點擊圖片中的文字來進行校驗的驗證碼,如圖 這種驗證碼驗證是驗證鼠標是否選中了圖片中文字的位置,以及選擇的順序,產生驗證碼的時候可以提供一組底圖,然后隨機獲取一張圖片,隨機選取幾個字,然后把文字的順
推薦度:
標簽: 圖片 驗證碼 實現
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top 主站蜘蛛池模板: 毛片综合 | 91国内精品久久久久免费影院 | 日本一区二区视频在线观看 | 最新国产在线播放 | 在线观看视频亚洲 | 午夜免费视频 | 五月婷婷啪啪 | 国产一级大片 | 中文字幕第一区 | 国产资源视频在线观看 | 国产成人综合久久精品下载 | 国产在线精品观看一区 | 国产精品成 | 国产欧美一区二区三区精品 | 国产区第一页 | 精品国产一区二区三区久久久狼 | 久久国产精品视频一区 | 91麻豆国产香蕉久久精品 | 国产经典一区 | 亚洲欧美日韩中文v在线 | 国产欧美日本亚洲精品五区 | 亚洲欧美日韩综合精品网 | 99久久国产综合精品成人影院 | 中文国产成人精品久久久 | 久久久国产这里有的是精品 | 亚洲综合一区二区精品久久 | 亚洲欧美偷拍另类 | 国产午夜久久影院 | 国产一二区视频 | 最近中文字幕高清电影在线 | 91久久综合九色综合欧美98 | 夜夜操夜夜| 国产h片在线观看 | 欧美3p在线观看一区二区三区 | 丁香六月久久 | 小说区 亚洲 自拍 另类 | 全免费a级毛片免费毛视频 热re91久久精品国产91热 | 国产精品免费播放 | 国产不卡在线看 | 亚洲色图欧美在线 | 亚洲另类网 |