驗證碼的作用
有效防止這種問題對某一個特定注冊用戶用特定程序暴力破解方式進行不斷的登陸嘗試,實際上是用驗證碼是現(xiàn)在很多網(wǎng)站通行的方式,利用比較簡易的方式實現(xiàn)了這個功能。雖然登陸麻煩一點,但是對社區(qū)還來說這個功能還是很有必要,也很重要。
驗證碼一般是防止批量注冊的,人眼看起來都費勁,何況是機器。二像百度貼吧未登錄發(fā)貼要輸入驗證碼大概是防止大規(guī)模匿名回帖的發(fā)生目前,不少網(wǎng)站為了防止用戶利用機器人自動注冊、登錄、灌水,都采用了驗證碼技術(shù)。所謂驗證碼,就是將一串隨機產(chǎn)生的數(shù)字或符號,生成一幅圖片, 圖片里加上一些干擾象素(防止OCR),由用戶肉眼識別其中的驗證碼信息,輸入表單提交網(wǎng)站驗證,驗證成功后才能使用某項功能。
具體實現(xiàn)
封裝的helper類,拿來用就行
/// <summary>
/// 生成驗證碼的類
/// </summary>
public class ValidateCode
{
/// <summary>
/// 生成驗證碼
/// </summary>
/// <param name="length">指定驗證碼的長度</param>
/// <returns></returns>
public string CreateValidateCode(int length)
{
var codeSerial = "2,3,4,5,6,7,8,a,b,c,d,e,f,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";//自定義隨機碼字符串序列(使用逗號分隔)
string[] arr = codeSerial.Split(',');
var validateNumberStr = "";//驗證碼
int randValue = -1;//隨機數(shù)
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
//生成驗證碼
for (int i = 0; i < length; i++)
{
randValue = rand.Next(0, arr.Length - 1);
validateNumberStr += arr[randValue].ToString();
}
return validateNumberStr;
}
/// <summary>
/// 創(chuàng)建驗證碼的圖片
/// </summary>
/// <param name="containsPage">要輸出到的page對象</param>
/// <param name="validateNum">驗證碼</param>
public byte[] CreateValidateGraphic(string validateCode)
{
Bitmap image = CreateImageCode(validateCode);
//保存圖片數(shù)據(jù)
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//輸出圖片流
return stream.ToArray();
}
/// <summary>
/// 生成校驗碼圖片
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public Bitmap CreateImageCode(string code)
{
int Length = code.Length;//驗證碼長度
int fSize = 40;//驗證碼字體大小
int FontSize = 40;
int Padding = 2;//邊框補
int fWidth = fSize + Padding;
int imageWidth = (int)(code.Length * fWidth) + 4 + Padding * 2;
int imageHeight = fSize * 2 + Padding;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(imageWidth, imageHeight);
Graphics g = Graphics.FromImage(image);
Color BackgroundColor = Color.White;//自定義背景顏色
Color ChaosColor = Color.LightGray;//噪點顏色
Color[] Colors = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//自定義隨機顏色數(shù)組
string[] Fonts = { "Arial", "Georgia" };//自定義字體數(shù)組
bool chaos = true;//是否輸出噪點
g.Clear(BackgroundColor);
Random rand = new Random();
//給背景添加隨機生成的燥點
if (chaos)
{
Pen pen = new Pen(ChaosColor, 0);
int c = Length * 10;
for (int i = 0; i < c; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(pen, x, y, 1, 1);
}
}
int left = 0, top = 0, top1 = 1, top2 = 1;
int n1 = (imageHeight - FontSize - Padding * 2);
int n2 = n1 / 4;
top1 = n2;
top2 = n2 * 2;
Font f;
Brush b;
int cindex, findex;
//隨機字體和顏色的驗證碼字符
for (int i = 0; i < code.Length; i++)
{
cindex = rand.Next(Colors.Length - 1);
findex = rand.Next(Fonts.Length - 1);
f = new System.Drawing.Font(Fonts[findex], fSize, System.Drawing.FontStyle.Bold);
b = new System.Drawing.SolidBrush(Colors[cindex]);
if (i % 2 == 1)
{
top = top2;
}
else
{
top = top1;
}
left = i * fWidth;
g.DrawString(code.Substring(i, 1), f, b, left, top);
}
//畫一個邊框 邊框顏色為Color.Gainsboro
g.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, image.Width - 1, image.Height - 1);
g.Dispose();
//產(chǎn)生波形(Add By 51aspx.com)
image = TwistImage(image, true, 8, 4);
return image;
}
private const double PI = 3.1415926535897932384626433832795;
private const double PI2 = 6.283185307179586476925286766559;
/// <summary>
/// 正弦曲線Wave扭曲圖片(Edit By 51aspx.com)
/// </summary>
/// <param name="srcBmp">圖片路徑</param>
/// <param name="bXDir">如果扭曲則選擇為True</param>
/// <param name="nMultValue">波形的幅度倍數(shù),越大扭曲的程度越高,一般為3</param>
/// <param name="dPhase">波形的起始相位,取值區(qū)間[0-2*PI)</param>
/// <returns></returns>
public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width, srcBmp.Height);
// 將位圖背景填充為白色
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), 0, 0, destBmp.Width, destBmp.Height);
graph.Dispose();
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
for (int i = 0; i < destBmp.Width; i++)
{
for (int j = 0; j < destBmp.Height; j++)
{
double dx = 0;
dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得當前點的顏色
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue);
System.Drawing.Color color = srcBmp.GetPixel(i, j);
if (nOldX >= 0 && nOldX < destBmp.Width
&& nOldY >= 0 && nOldY < destBmp.Height)
{
destBmp.SetPixel(nOldX, nOldY, color);
}
}
}
return destBmp;
}
}
該文章在 2024/4/1 17:07:15 編輯過