asp.net系統,在登入或注冊時常提供數字或字元混合的驗證碼,這裡介紹如何操作。
1,Default 頁面
<%@ Page Language="C#" %>
<script runat="server">
protected void Button2_Click(object sender, EventArgs e)
{
if (Request.Cookies["ycode"] != null)
{
if (Request.Cookies["ycode"].Value.ToString() == this.TextBox1.Text.Trim())
{
Response.Redirect("Default2.aspx");
}
}
}
</script>
<head runat="server">
<title></title>
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/JScript.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img id="img1" src="CreateCode.aspx" alt="點選" onclick="hp()"/>
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></div>
</form>
</body>
</html>
2 ,CreateCode 頁面
<%@ Import Namespace="System.Drawing" %>
protected void Page_Load(object sender, EventArgs e)
int nLen = 6;
int nBmpWidth = 13 * nLen + 10;
int nBmpHeight = 30;
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth, nBmpHeight);
// 1. 生成随機背景顔色
int nRed, nGreen, nBlue; // 背景的三元色
System.Random rd = new Random((int)System.DateTime.Now.Ticks);
nRed = rd.Next(255) % 128 + 128;
nGreen = rd.Next(255) % 128 + 128;
nBlue = rd.Next(255) % 128 + 128;
// 2. 填充位圖背景
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed, nGreen, nBlue))
, 0
, nBmpWidth
, nBmpHeight);
// 3. 繪制幹擾線條,采用比背景略深一些的顔色
int nLines = 3;
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed - 17, nGreen - 17, nBlue - 17), 2);
for (int a = 0; a < nLines; a++)
int x1 = rd.Next() % nBmpWidth;
int y1 = rd.Next() % nBmpHeight;
int x2 = rd.Next() % nBmpWidth;
int y2 = rd.Next() % nBmpHeight;
graph.DrawLine(pen, x1, y1, x2, y2);
// 采用的字元集,可以随即拓展,并可以控制字元出現的幾率
string strCode = "abcdefghijklmcopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 4. 循環取得字元,并繪制
string strResult = "";
for (int i = 0; i < nLen; i++)
int x = (i * 13 + rd.Next(3));
int y = rd.Next(4) + 1;
// 确定字型
System.Drawing.Font font = new System.Drawing.Font("宋體",
16 + rd.Next() % 4,
System.Drawing.FontStyle.Bold);
char c = strCode[rd.Next(strCode.Length)]; // 随機擷取字元
strResult += c.ToString();
// 繪制字元
graph.DrawString(c.ToString(),
font,
new SolidBrush(System.Drawing.Color.FromArgb(nRed - 60 + y * 3, nGreen - 60 + y * 3, nBlue - 40 + y * 3)),
x,
y);
// 5. 輸出位元組流
System.IO.MemoryStream bstream = new System.IO.MemoryStream();
bmp.Save(bstream, System.Drawing.Imaging.ImageFormat.Jpeg);
bmp.Dispose();
graph.Dispose();
//将正确的字儲存到Cookie
Response.Cookies["ycode"].Value = strResult;
byte[] byteReturn = bstream.ToArray();
bstream.Close();
Response.OutputStream.Write(byteReturn,0,byteReturn.Length);
</div>
3,js 其中用到了jquery 技術
function hp()
{
$("#img1").attr("src","CreateCode.aspx?r="+Math.random());
}
function Button1_onclick()
var cookieString = new String(document.cookie);
var start=cookieString.indexOf("=");
var k=cookieString.substr(start+1,cookieString.length);
if($("#Text1").val()==k)
{
alert("成功!");
}
else
alert("驗證碼不正确!");