.Net Core 實(shí)現(xiàn)圖片驗(yàn)證碼的實(shí)現(xiàn)示例
記錄自己的學(xué)習(xí),參考了網(wǎng)上各位大佬的技術(shù),往往在登錄的時(shí)候需要使用到驗(yàn)證碼來(lái)進(jìn)行簡(jiǎn)單的一個(gè)校驗(yàn),這邊使用在.net core上進(jìn)行生成圖片二維碼
思路很簡(jiǎn)單=》 生成一個(gè)隨機(jī)數(shù)-》保存到服務(wù)端Session-》生成隨機(jī)碼對(duì)應(yīng)的圖片給前端-》登錄的時(shí)候進(jìn)行校驗(yàn)(也可以在后端進(jìn)行隨機(jī)碼的token加密,存到Cooick里面在前端進(jìn)行校驗(yàn))
第一步:生成隨機(jī)數(shù)
private static string RndNum(int VcodeNum)
{
//驗(yàn)證碼可以顯示的字符集合
string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p" +
",q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,P,Q" +
",R,S,T,U,V,W,X,Y,Z";
string[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成數(shù)組
string code = "";//產(chǎn)生的隨機(jī)數(shù)
int temp = -1;//記錄上次隨機(jī)數(shù)值,盡量避避免生產(chǎn)幾個(gè)一樣的隨機(jī)數(shù)
Random rand = new Random();
//采用一個(gè)簡(jiǎn)單的算法以保證生成隨機(jī)數(shù)的不同
for (int i = 1; i < VcodeNum + 1; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));//初始化隨機(jī)類
}
int t = rand.Next(61);//獲取隨機(jī)數(shù)
if (temp != -1 && temp == t)
{
return RndNum(VcodeNum);//如果獲取的隨機(jī)數(shù)重復(fù),則遞歸調(diào)用
}
temp = t;//把本次產(chǎn)生的隨機(jī)數(shù)記錄起來(lái)
code += VcArray[t];//隨機(jī)數(shù)的位數(shù)加一
}
return code;
}
第二步:生成驗(yàn)證碼圖片
public static MemoryStream Create(out string code, int numbers = 4)
{
code = RndNum(numbers);
//Bitmap img = null;
//Graphics g = null;
MemoryStream ms = null;
Random random = new Random();
//驗(yàn)證碼顏色集合
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//驗(yàn)證碼字體集合
string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };
using (var img = new Bitmap((int)code.Length * 18, 32))
{
using (var g = Graphics.FromImage(img))
{
g.Clear(Color.White);//背景設(shè)為白色
//在隨機(jī)位置畫背景點(diǎn)
for (int i = 0; i < 100; i++)
{
int x = random.Next(img.Width);
int y = random.Next(img.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
//驗(yàn)證碼繪制在g中
for (int i = 0; i < code.Length; i++)
{
int cindex = random.Next(7);//隨機(jī)顏色索引值
int findex = random.Next(5);//隨機(jī)字體索引值
Font f = new Font(fonts[findex], 15, FontStyle.Bold);//字體
Brush b = new SolidBrush(c[cindex]);//顏色
int ii = 4;
if ((i + 1) % 2 == 0)//控制驗(yàn)證碼不在同一高度
{
ii = 2;
}
g.DrawString(code.Substring(i, 1), f, b, 3 + (i * 12), ii);//繪制一個(gè)驗(yàn)證字符
}
ms = new MemoryStream();//生成內(nèi)存流對(duì)象
img.Save(ms, ImageFormat.Jpeg);//將此圖像以Png圖像文件的格式保存到流中
}
}
return ms;
}
第三步:控制器調(diào)用方法生成隨機(jī)數(shù)圖片之后,進(jìn)行隨機(jī)數(shù)的保存
HttpContext.Session.SetString("LoginValidateCode", code);
備注:在使用Session的時(shí)候要進(jìn)行Session服務(wù)的注冊(cè)
在ConfigureServices中services.AddSession();
在Configure中app.UseSession();
最后在前端進(jìn)行驗(yàn)證碼圖片的綁定
<img style="justify-content:center" id="code" src="/Users/Login/GetVerifyCode" />
點(diǎn)擊圖片進(jìn)行驗(yàn)證碼刷新

function GetCode() {
$.ajax({
type: "GET",
url: "/Users/Login/GetVerifyCode",
data: {},
dataType: "json",
success: function (data) {
},
complete: function () {
$("#code").attr('src', '/Users/Login/GetVerifyCode')
}
});
}
SkiaSharp
這個(gè)百度上的搜索結(jié)果沒(méi)有一個(gè)是給了可用代碼的。ε=(´ο`*)))唉 而且大部分都是一個(gè)人放出來(lái)的代碼 好吧開始自己整。先上代碼
public IActionResult Code()
{
#region 反射SK支持的全部顏色
//List<SKColor> colors = new List<SKColor>();
//var skcolors = new SKColors();
//var type = skcolors.GetType();
//foreach (FieldInfo field in type.GetFields())
//{
// colors.Add( (SKColor)field.GetValue(skcolors));
//}
#endregion
//int maxcolorindex = colors.Count-1;
string text = "1A3V";
var zu = text.ToList();
SKBitmap bmp = new SKBitmap(80, 30);
using (SKCanvas canvas = new SKCanvas(bmp))
{
//背景色
canvas.DrawColor(SKColors.White);
using (SKPaint sKPaint = new SKPaint())
{
sKPaint.TextSize = 16;//字體大小
sKPaint.IsAntialias = true;//開啟抗鋸齒
sKPaint.Typeface = SKTypeface.FromFamilyName("微軟雅黑", SKTypefaceStyle.Bold);//字體
SKRect size = new SKRect();
sKPaint.MeasureText(zu[0].ToString(), ref size);//計(jì)算文字寬度以及高度
float temp = (bmp.Width/4 - size.Size.Width)/2;
float temp1 = bmp.Height - (bmp.Height - size.Size.Height) / 2;
Random random = new Random();
for (int i = 0; i < 4; i++)
{
sKPaint.Color = new SKColor((byte)random.Next(0,255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
canvas.DrawText(zu[i].ToString(), temp + 20*i, temp1, sKPaint);//畫文字
}
//干擾線
for (int i = 0; i < 5; i++)
{
sKPaint.Color = new SKColor((byte)random.Next(0, 255), (byte)random.Next(0, 255), (byte)random.Next(0, 255));
canvas.DrawLine(random.Next(0, 40), random.Next(1, 29), random.Next(41, 80), random.Next(1, 29), sKPaint);
}
}
//頁(yè)面展示圖片
using (SKImage img = SKImage.FromBitmap(bmp))
{
using (SKData p = img.Encode())
{
return File(p.ToArray(), "image/Png");
}
}
}
}
到此這篇關(guān)于.Net Core 實(shí)現(xiàn)圖片驗(yàn)證碼的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān).Net Core 圖片驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
出處:https://www.cnblogs.com/net-open/
相關(guān)文章
ASP.NET Core SignalR中的流式傳輸深入講解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core SignalR中流式傳輸?shù)南嚓P(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
在Repeater控件中通過(guò)Eval的方式綁定Style樣式代碼
這篇文章主要介紹了如何在Repeater控件中通過(guò)Eval的方式綁定Style樣式,需要的朋友可以參考下2014-04-04
淺析GridView中顯示時(shí)間日期格式的問(wèn)題
下面小編就為大家?guī)?lái)一篇淺析GridView中顯示時(shí)間日期格式的問(wèn)題。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05
ASP.NET網(wǎng)頁(yè)打印(只打印相關(guān)內(nèi)容/自寫功能)
朋友要求在前段時(shí)間完成的新聞的網(wǎng)站上加上一個(gè)功能,就是在每篇新聞瀏覽的頁(yè)面, 加一個(gè)打印銨鈕。讓用戶一點(diǎn)打印,能把整篇文章打印2013-01-01
ASP.NET MVC傳送參數(shù)至服務(wù)端詳解及實(shí)例
這篇文章主要介紹了ASP.NET MVC傳送參數(shù)至服務(wù)端詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-11-11
asp.net Excel轉(zhuǎn)換為SQL Server的方法
辦公軟件Excel是一種常用的電子表格軟件,在編程項(xiàng)目中有需要將Excel轉(zhuǎn)換為SQL Server數(shù)據(jù)庫(kù)的需求,本文對(duì)此進(jìn)行一些介紹并給出設(shè)計(jì)代碼。2009-06-06
asp.net 通過(guò)指定IP地址得到當(dāng)前的網(wǎng)絡(luò)上的主機(jī)的域名
通過(guò)指定的ip地址獲取當(dāng)前網(wǎng)絡(luò)的主機(jī)的域名,大家可以看看2009-02-02

