各位朋友们,请问怎么实现验证码验证呀? 有什么办法能实现验证码验证呢? 验证码是一个image控件加载一个checkcode.aspx文件。 请问怎么实现验证呢? 都有什么方法呢? 最近折磨我几天了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://blog.csdn.net/guoyz_1/article/details/3314153http://www.cnblogs.com/Leo_wl/archive/2010/05/18/1738095.html 是动态实现验证码?具体说清楚点,最总问题是什么,可以使用src='checkcode.ashx'文件做 ashx文件中生成一个随机数,或者是随机字母数字的组合,再将其转换为图片。有了图片就和插入图片一样的HTML代码了。从ashx中得到该随机数和前台用户的输入做对比,如果正确则通过验证,不对就让其重新输入。还可以做出点击图片重新生成验证码的效果,因为有时候用户看不清楚你生成的验证码,想重新生成一下。 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 Common_ValidateCode : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { CreateCheckCodeImage(GenerateCheckCode()); } private string GenerateCheckCode() { int number; char code; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < 5; i++) { number = random.Next(); //if (number % 2 == 0) code = (char)('0' + (char)(number % 10)); //else // code = (char)('A' + (char)(number % 26)); checkCode += code.ToString(); } //cookie判断 //Response.Cookies.Add(new HttpCookie("CheckCode", checkCode)); Session["vcode"] = checkCode; return checkCode; } private void CreateCheckCodeImage(string checkCode) { if (checkCode == null || checkCode.Trim() == String.Empty) return; 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 < 10; 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.Silver), x1, y1, x2, y2); } Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)); 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"; Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } }}引用 <img src="ValidateCode.aspx" onclick="this.src=this.src+'?Math.random()';"/> 这是页面代码 我正在用的。 你把那个aspx文件就当成一个图片 路径赋值给 image 的scr属性 让它显示出验证码就行了啊 就这么简单,,,这个aspx文件有个session值啊 后台吧用户输入的跟这个session一比较 不就实现是否验证了么 在生成图片的同时把验证码写到页面或者Session中,当然不能去图片取值了 不知道楼主用webform还是mvc,我写过一个ASP.NET MVC下用的验证控件:http://www.webdiyer.com/Controls/MvcCaptcha 原理:在checkcode.aspx中生成随机数保存到Session中,然后用GDI+绘图输出BitMap.在验证验证码的地方取出Session值和输入的值进行比较. 为什么无法获取repeater内的linkbutton? 数据库插入日期与当前日期,比较是否相同 求救,如何在页面中在线打开所有格式的文件,而不下载?? 请教大家。在VS.NET中写好程序怎么跟美工结合呢??? 请高手给个.net发送外部邮件的成功例子呗 请问同一解决方案里面的两个不同的项目之间可不可以互用对方的用户控件? 求quictstart 寻代码!再线等 往数据库传二进制流,怎么传 agsxmpp 怎么添加到项目里面去~~求攻略 UMSMonitor流媒体服务软件如果使用? SQL去除重复列
还可以做出点击图片重新生成验证码的效果,因为有时候用户看不清楚你生成的验证码,想重新生成一下。
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 Common_ValidateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateCheckCodeImage(GenerateCheckCode());
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < 5; i++)
{
number = random.Next(); //if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
//else
// code = (char)('A' + (char)(number % 26)); checkCode += code.ToString();
} //cookie判断
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
Session["vcode"] = checkCode;
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return; 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 < 10; 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.Silver), x1, y1, x2, y2);
} Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
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";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
引用
<img src="ValidateCode.aspx" onclick="this.src=this.src+'?Math.random()';"/>
这是页面代码 我正在用的。
在checkcode.aspx中生成随机数保存到Session中,然后用GDI+绘图输出BitMap.在验证验证码的地方取出Session值和输入的值进行比较.