C# 验证码程序及其验证(没有错误,已经验证过)
第一步 :一个页面只在后台生成验证码图片程序代码 :
using System.Drawing ;
using System.Drawing .Imaging ;
using System.IO ;
protected void Page_Load(object sender, EventArgs e)
{
string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
Color[] color ={ Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange,
Color.Brown, Color.DarkBlue,Color .BurlyWood ,Color .Chocolate,Color.DarkGray };
//字体列表,用于验证码
string[] font ={ "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh",
"PMingLiU"};
//验证码的字符集,去掉了一些容易混淆的字符
char[] character ={ '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串
for (int i = 0; i < 4; i++)
{
chkCode += character[rnd.Next(character.Length)];
}
//保存验证码的Cookie
HttpCookie anycookie = new HttpCookie("validateCookie", chkCode);
Response.Cookies.Add(anycookie);
Bitmap bmp = new Bitmap(150, 30);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
//画噪线
for (int i = 0; i < 4; i++) //控制噪线的个数
{
int x1 = rnd.Next(150);
int y1 = rnd.Next(30);
int x2 = rnd.Next(150);
int y2 = rnd.Next(30);
Color clr = color[rnd.Next(color.Length)];
g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)//控制验证码的个数
{
string fnt = font[rnd.Next(font.Length)];
Font ft = new Font(fnt, 16);
Color clr = color[rnd.Next(color.Length)];
g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 30 +
25, (float)5);
}
//画噪点
for (int i = 0; i < 60; i++) //控制躁点的个数
{
int x = rnd.Next(bmp.Width);
int y = rnd.Next(bmp.Height);
Color clr = color[rnd.Next(color.Length)];
bmp.SetPixel(x, y, clr);
}
//清除该页输出缓存,设置该页无缓存
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
//将验证码图片写入内存流,并将其以"p_w_picpath/Png" 格式输出
MemoryStream ms = new MemoryStream();
try
{
bmp.Save(ms, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "p_w_picpath/Png";
Response.BinaryWrite(ms.ToArray());
}
finally
{
//显式释放资源
bmp.Dispose();
g.Dispose();
}
}
第二步:在另外一个页面 前台代码:js代码是用js验证cookie
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" >
function check(objName)
{
var arrStr = document.cookie.split("; ");
for(var i = 0;i < arrStr.length;i ++){
var temp = arrStr[i].split("=");
if(temp[0] == objName){
var s= unescape(temp[1]);
var s1=document.getElementById("TextBox1").value;
if (s.toLowerCase( ) ==s1.toLowerCase( ))
{
alert("验证码正确");
return true;
}
else
{
alert("验证码错误");
return false;
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="yzNew.aspx" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button2" type="button" value="检验验证码" return check('validateCookie')"/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Label ID="Label1" runat="server" Width="126px"></asp:Label></div>
</form>
</body>
</html>
第三步:后台程序:用c#验证输入的验证码是否一致
protected void Button1_Click(object sender, EventArgs e)
{
string text = this.TextBox1.Text.ToString();//获得用户输入的验证码
string chkcode = Request.Cookies["validateCookie"].Values["ChkCode"].ToString();//获得系统生成的验证码
if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(chkcode))
{
if (chkcode.ToUpper().ToString() == text.ToUpper())
{
Label1.Text = "验证码正确";
}
else
{
Label1.Text = "验证码输入不正确";
}
}
}
转载于:https://blog.51cto.com/chashui/585151