读书人

如何判断验证码是否输入正确

发布时间: 2013-09-08 15:21:21 作者: rapoo

怎么判断验证码是否输入正确?

//CheckCode.aspx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Configuration;
using System.Web.Security;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
// 首先调用RndNum函数生成4位数字的验证码
string str_ValidateCode = RndNum(4);
// 然后把它保存在用于验证的Session里边
Session["ValidateCode"] = str_ValidateCode;
//调用CreateImage函数,把验证码绘制到图片中
CreateImage(str_ValidateCode);
}
}
private string RndNum(int VcodeNum)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
",q,r,s,t,u,v,w,x,y,z";
string[] VcArray = Vchar.Split(new Char[] { ',' });
string VNum = "";
//int temp = -1;
Random rand = new Random();
for (int i = 1; i < VcodeNum + 1; i++)
{

int t = rand.Next(36);
VNum += VcArray[t];
}
return VNum;
}
//生成随机颜色

private Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);


// 对于C#的随机数,没什么好说的
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
// 为了在白色背景上显示,尽量生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue);
}
public void CreateImage(string str_ValidateCode)
{
int int_ImageWidth = str_ValidateCode.Length * 13;
Random newRandom = new Random();
// 图高20px
Bitmap theBitmap = new Bitmap(int_ImageWidth, 20);
Graphics theGraphics = Graphics.FromImage(theBitmap);
// 白色背景
theGraphics.Clear(Color.White);
// 灰色边框
theGraphics.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, int_ImageWidth - 1, 19);
// 10pt的字体
Font theFont = new Font("Arial", 10);
for (int int_index = 0; int_index < str_ValidateCode.Length; int_index++)
{
string str_char = str_ValidateCode.Substring(int_index, 1);
Brush newBrush = new SolidBrush(GetRandomColor());
Point thePos = new Point(int_index * 13 + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
theGraphics.DrawString(str_char, theFont, newBrush, thePos);
}


// 将生成的图片发回客户端
MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, ImageFormat.Jpeg);

Response.ClearContent(); //需要输出图象信息 要修改HTTP头
Response.ContentType = "image/Png";
Response.BinaryWrite(ms.ToArray());
theGraphics.Dispose();
theBitmap.Dispose();
Response.End();


}
}
我在CheckCode.aspx.cs中加了上面这段代码,目前能实现的是有验证码显示。但没有判断语句。
我在Login.aspx中加 <script type="text/javascript"> 
 function checkwd_reload() 
 {  var ob = document.getElementById("chk_img");  
ob.src = "CreateImage.aspx?"+ new Date();  }  
</script>  
<asp:TextBox ID="txtVerifyCode" runat="server" Width="150px"></asp:TextBox>  
<img id="chk_img" onclick="checkwd_reload()" style="cursor: pointer;" align="absbottom" src="CreateImage.aspx" alt="如何判断验证码是否输入正确" />//点击刷新验证码 
 <span id="message" style="color:red;font-size: 11px " runat="server"></span>  
在Login.aspx.cs中加
string code = this.txtVerifyCode.Text; //输入验证码 
 string scode = Session["CheckCode"].ToString(); //获取验证码 
 if (code != scode) 
 {  message.InnerHtml = "请输入正确的验证码!";  }  
else 
 {  //登录成功!  }
这个判断语句,它总说当前上下文中不存在txtVerifyCode和message,可我在Login.aspx中添加了Textbox和Label啊,ID也是一一对应的,不知道为什么它不识别,求指教!! 验证码
[解决办法]
验证码图片里面的字母是系统随机生成的,它被放到session里面,当用户提交时,就用session面存储的信息与用户输入的信息比较,相等则验证通过,否则....

读书人网 >C#

热点推荐