C#.NET画验证码与Cookie验证

时间:2023-02-03 08:23:09
新建页面Code.aspx 在里面话验证码 后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateCheckCodeImage(GenerateCheckCode());
}
}
// 产生四位随机数的方法
private string GenerateCheckCode()
{
int number;
char code;
//表示字符串为空,次字段为只读
string checkCode = string.Empty;
Random random = new Random();
for (int i = 0; i < 4; i++)
{
number = random.Next();
code = (char)('0' + (char)(number % 10));
checkCode += code.ToString();
}
//将验证码保存到cookie中
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
//当随机数是空的时候或者 去掉空格还是空的时候
if (checkCode == null || checkCode.Trim() == string.Empty)
return;
// 创建位图 4 * 12.5
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);

try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的毕竟噪音线
for (int i = 0; i < 2; i++)
{
//产生随机的起始坐标和重点坐标
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
//字体 大小12 加粗
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
// 线性渐变封装
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
// 画图片的前景噪点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
// 花图片的边框线 矩形
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//创建其支持存储区为内存的
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
// 清楚缓冲区流中的所有内容输出
Response.ClearContent();
// 获取或设置输出流的格式
Response.ContentType = "image/gif";
//将一个二进制字符串 写入输出流 ms.ToArray()将整个流内容写入数组
Response.BinaryWrite(ms.ToArray());

}
finally
{
// 释放资源
g.Dispose();
image.Dispose();
}
}
}
展示的页面:

验证码用HTML标签ima表示的 src的路径制定到Code.aspx
当点击图片的时候自动的刷新图片的路径 达到更换验证码的目的.。是通过js刷新img的src的路径实现的
<img id="Img1" alt="看不清,请点击我!" onclick="this.src=this.src+'?'" src="Code.aspx" style="width: 73px; height: 22px" align="left" />


验证页面的后台代码:

    protected void Button1_Click(object sender, EventArgs e)
{
string code = this.txtCode.Text;
//从cookie中取出验证码
HttpCookie cookie = Request.Cookies["CheckCode"];
if (code == cookie.Value.ToString())
{
Response.Write("正确");
}
else
{
Response.Write("错误");
}
}
将验证码保存到cookie中:Response.Cookies.Add(new HttpCookie("CheckCode",checkCode));
从 cookie中取出验证码:HttpCookie cookie = Request.Cookies["CheckCode"];