当前位置:编程学习 > C#/ASP.NET >>

如何判断验证码是否正确

我做了一个页面,不知道如何判断验证码是否正确?
验证码页面:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace DiplomaProject.IdentifyingCode
{
    public partial class Code : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   GenerateCheckCode();//调用验证嘛函数,页面加载生成字符;            
            CreateCheckCodeImage(GenerateCheckCode());//将字符加到背景中去;
        }
        private String GenerateCheckCode()  //产生验证码
        {   int number;
            char code;
            string checkCode = String.Empty;
            System.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();

            }
            Session["CheckCode"] = checkCode;
            return checkCode;
        }
        private void CreateCheckCodeImage(string checkCode)//用于产生背景图像
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return;
            System.Drawing.Bitmap image = new Bitmap(150,50);

            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 = new System.Drawing.Font("Arial", 40, (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();
            }
        }
    }
}
登录页面的aspx:
<img src="IdentifyingCode/Code.aspx" id="yzm" onclick="this.src='IdentifyingCode/Code.aspx?abc='+Math.random()" alt="图片看不清?点击重新得到验证码" style="cursor:hand;" />
登录页面的aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PYQ.BLL;
using PYQ.Model;

namespace DiplomaProject
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ltlShowMessageBox.Text = null;
            Session.Clear();
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {   
            this.ltlShowMessageBox.Text = null;
            AdminInfo info = new AdminInfo();
            info.UserName = this.txtAdminName.Text;
            info.UserPwd = this.txtAdminPaw.Text;
            PYQ.BLL.Admin obj = new Admin();
            try
            {
                switch (obj.Login(ref info))
                {
                    case Admin.LoginType.UserNameNoExists: 
                        this.ltlShowMessageBox.Text = "<script>alert('用户名不存在!');</script>";
                        Session["Refurbish"] = null;
                        break;
                    case Admin.LoginType.ErrUserPwd:
                        this.ltlShowMessageBox.Text = " <script>alert('密码错误!');</script>";
                        Session["Refurbish"] = null;
                        break;
                    case Admin.LoginType.LoginSuccess:
                        Session["IamOk"] = "YouAreOk";
                        Session["Refurbish"] = "1";
                        Session["AdminName"] = this.txtAdminName.Text.ToString();
                        Response.Redirect("SCS/SystemFrame.aspx");
                        break;
                }
            }
            catch (Exception E)
            {
                Response.Write(E.ToString());
            }
        }      
    }
}
请问登录页面的aspx.cs如何做判断? --------------------编程问答-------------------- 在线等哦。。。。。。。。。。。。。。。。 --------------------编程问答-------------------- 学习中!!!!! --------------------编程问答-------------------- 还是没人来............... --------------------编程问答-------------------- 哎,你不是存了Session["CheckCode"] = checkCode; 
登录是判断下不就OK?
判断输入的内容与session["CheckCode"]是否相同就行了啊? --------------------编程问答--------------------

 protected void Button1_Click(object sender, EventArgs e)
 {
     if (String.Compare(Request.Cookies["CheckCode"].Value, TextBox3.Text.ToString().Trim(), true) != 0)
            {
                sm.ShowLocation("验证码输入有误。");
            }
     else
        {

        }
 }
      
--------------------编程问答--------------------
引用 5 楼 z272635035 的回复:
C# code

 protected void Button1_Click(object sender, EventArgs e)
 {
     if (String.Compare(Request.Cookies["CheckCode"].Value, TextBox3.Text.ToString().Trim(), true) != 0)
            {
         ……
--------------------编程问答--------------------
引用 4 楼 kart15 的回复:
哎,你不是存了Session["CheckCode"] = checkCode;
登录是判断下不就OK?
判断输入的内容与session["CheckCode"]是否相同就行了啊?
  Session["CheckCode"]=GenerateCheckCode(); 
然后按 5楼的. --------------------编程问答-------------------- 楼上正解
你在点击登陆的时候判断文本框的值和Session中的值是否一致就行了撒 --------------------编程问答-------------------- 使用session啊 --------------------编程问答-------------------- 用session存起来。再跟输入的比对。 --------------------编程问答-------------------- 在
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();

}
在点击登陆的时候判断文本框的值和Session中的值是否一致就行

之后加 session。Add("XX",checkCode);

--------------------编程问答-------------------- 顶
up
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,