登录页的验证码

时间:2021-12-01 17:10:11

刚开始为登录页添加验证码功能时,把验证码代码写在了登录页的后台代码,运行起来只有验证码显示,别的没有了。才知道验证码是要另外写在一个页,调用就可以了。

登录页就那点功能,别的代码也不写了,就写个验证码的吧。

Login.aspx页的代码

 <label style="text-align:right" >验证码</label>
  <asp:TextBox style="width:50px;float:left;margin-left:18px"></asp:TextBox>
 <input class="text-input" name="CheckCode" type="text"
style="width:50px;float:left;margin-left:18px" />
<img src="CheckCode.aspx" style="margin-left:18px" alt="点击刷新验证码"
 onclick="this.src='CheckCode.aspx?rnd='+Math.random();" />

login.aspx.cs页的代码就不写了,主要就是登录的逻辑代码。



下面是验证码页的代码

CheckCode.aspx页没有代码。需要一张透明的图片,用来显示验证码。

CheckCode.aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Collections;

public partial class System_CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//把生成的验证码字符串赋值给code
string code = GenerateCheckCode();
//用Cookie保存验证码,一分钟过期
HttpCookie CheckCode = new HttpCookie("CheckCode", code);
CheckCode.Expires = new DateTime(6000);
Response.AppendCookie(CheckCode);
//根据生成的验证码绘制图片
CreatCheckCodeImage(CheckCode.Value);
}
//生成验证码字符串
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();

//为生成随机数设置条件
if (number % 2 == 0)
{
code = (char)('0' + (char)(number % 10));
}
else
{
code = (char)('A' + (char)(number % 26));
}
//完整验证码
checkCode += code.ToString();
}
return checkCode;

}


//根据传入的验证码字符串生成图片
private void CreatCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == string.Empty) return;
Bitmap image = new Bitmap((int)(Math.Ceiling(checkCode.Length * 17.5)), 28);

Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Width);
int y2 = random.Next(image.Width);
//绘制一条由坐标对指定的两个点的线条
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);

}

//设置字体样式 使用指定的大小和样式初始化新Font
Font font = new Font("Arial", 17, (FontStyle.Bold | FontStyle.Italic));

//使用线性渐变封装 Brush。此类不能被继承。
//Rectangle(Int32, Int32, Int32, Int32)用指定的位置和大小初始化 Rectangle 类的新实例
LinearGradientBrush brush =
 new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
//DrawString(String, Font, Brush, Single, Single)
//在指定位置并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。
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) 获取此 Bitmap 中指定像素的颜色。
// FromArgb(Int32)从一个 32 位 ARGB 值创建 Color 结构。
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}

//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());

}
finally
{
g.Dispose();
image.Dispose();
}

}
}

这样去运行登录页,验证码点击就可刷新了。