验证码是大家常用的功能,主要是防止非法用户或不怀好意的暴力登录系统。现在就介绍利用ashx一般处理程序来生成随机验证码。
1.新建ashx文件,并继承接口:IHttpHandler,IRequiresSessionState。
1)生成数字和字母的随机字符串
/// <summary>
/// 生成验证码字符串(字母和数字组合,也可以拓展到汉字)
/// </summary>
/// <param name="codeLen">验证码字符长度</param>
/// <returns>返回验证码字符串</returns>
private string MakeCode(int codeLen)
{
if (codeLen < 1)
{
return string.Empty;
}
int number;
string checkCode = string.Empty;
Random random = new Random();
for (int index = 0; index < codeLen; index++)
{
number = random.Next();
if (number % 2 == 0)
{
checkCode += (char)('0' + (char)(number % 10)); //生成数字
}
else
{
checkCode += (char)('A' + (char)(number % 26)); //生成字母
}
}
return checkCode;
}
2)生成带字符串的验证码图片流
///<summary>
/// 获取验证码图片流
/// </summary>
/// <param name="checkCode">验证码字符串</param>
/// <returns>返回验证码图片流</returns>
private MemoryStream CreateCodeImg(string checkCode)
{
if (string.IsNullOrEmpty(checkCode))
{
return null;
}
Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics graphic = Graphics.FromImage(image);
try
{
Random random = new Random();
graphic.Clear(Color.White);
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for (int index = 0; index < 25; index++)
{
x1 = random.Next(image.Width);
x2 = random.Next(image.Width);
y1 = random.Next(image.Height);
y2 = random.Next(image.Height);
graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true);
graphic.DrawString(checkCode, font, brush, 2, 2);
int x = 0;
int y = 0;
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
x = random.Next(image.Width);
y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//将图片验证码保存为流Stream返回
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms;
}
finally
{
graphic.Dispose();
image.Dispose();
}
3)在public void ProcessRequest (HttpContext context1) 中调用
string chkCode = string.Empty;
HttpContext context;
public void ProcessRequest (HttpContext context1) {
this.context = context1;
//生成四位的随机码
string chkCode = MakeCode(4);
//保存Session中,以便方便验证
context.Session["chkCode"] = chkCode;
//无需缓存
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ClearContent();
//设置图片类型
context.Response.ContentType = "image/Png";
MemoryStream ms = CreateCodeImg(chkCode);
if (null != ms)
{
context.Response.BinaryWrite(ms.ToArray());
}
}
2.web页面调用ashx文件,生成验证码:
,在web文件中定义一个img控件,然后在scr属性中指向ashx文件。
<img id="img" title="点击这里换一个" src="../Action/tools/VerificationCode.ashx" />,利用jquery,给img添加点击事件,实现点击刷新验证码。
$("#img").click(function () {
$("img").attr("src","").attr("src", "../action/tools/VerificationCode.ashx");
})
启动页面就可以看到验证码,但按照上面的方法添加点击事件时只可以点击一次进行刷新,以后再点击验证码不变。
3.解决验证码只能点击一次的问题
将上面的点击事件方法变更为: $("img").attr("src","").attr("src", "../action/tools/VerificationCode.ashx?xx="+new Date());即可实现每次点击都刷新验证码。