關於產生驗證碼的源碼以及問題解決

來源:互聯網
上載者:User
解決|問題|驗證碼

在網上找了一個驗證碼的原始碼,使用後出現如下問題:不管你輸入正確與否,都會提示驗證碼錯誤。後來經過在qq群裡和csdn發貼諮詢,得知是ie瀏覽器緩衝問題。在經過他人的指點後改正了錯誤。原始碼如下:

-------------------Validate.aspx------------------

<%@ Page Language="C#" %>
<%@ import namespace="System"%>
<%@ import namespace="System.IO"%>
<%@ import namespace="System.Drawing"%>
<%@ import namespace="System.Drawing.Imaging"%>
<%@ import namespace="System.Drawing.Drawing2D"%>

<script runat="server">
private Bitmap validateimage;
private Graphics g;
 public void  Page_Load(object Sender   ,EventArgs e  ){
     Response.BufferOutput = true;   //特別注意
     Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-1));//特別注意
     Response.Cache.SetCacheability(HttpCacheability.NoCache);//特別注意
     Response.AppendHeader("Pragma", "No-Cache"); //特別注意
    string  VNum  =MakeValidateCode( );
 Session["VNum"]=VNum;//取得驗證碼,以便後來驗證
    ValidateCode(VNum);}
public void  ValidateCode(string VNum)
{
validateimage = new Bitmap(60, 20, PixelFormat.Format24bppRgb);
g = Graphics.FromImage(validateimage);
g.FillRectangle(new LinearGradientBrush(new Point(0,0), new Point(110,20), Color.FromArgb(240,255,255,255),Color.FromArgb(240,255,255,255)),0,0,200,200);
g.DrawString(VNum, new Font("arial",11),new SolidBrush(Color.Red),new PointF(6,0));
g.Save();
MemoryStream ms=new MemoryStream();
validateimage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType="image/bmp";
Response.BinaryWrite(ms.ToArray());
Response.End();
}

  string MakeValidateCode()
{
char[] s = new char[]{'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 num = "";
Random r = new Random();
for(int i = 0; i < 5; i++)
{
num += s[r.Next(0, s.Length)].ToString();
}
 return num;
}
</script>

-----------------------register.aspx------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="MyRegister.Register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>會員註冊</title>
    <LINK href="stylesheet.css" _fcksavedurl=""stylesheet.css"" _fcksavedurl=""stylesheet.css"" _fcksavedurl=""stylesheet.css"" type="text/css" rel="stylesheet">
</head>
<body>
    <form id="form1" runat="server">
    <div class="middle" style="height: 1px" >
    <div >
        <asp:Label ID="UserName" runat="server" Text="使用者名稱:" ></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    <div >
        <asp:Label ID="UserPsw" runat="server" Text="密碼:" ></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></div>
    <div >
        <asp:Label ID="Label2" runat="server" Text="確認密碼:" ></asp:Label>
        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></div>
    <div >
        <asp:Label ID="Question" runat="server" Text="找回密碼問題:"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></div>
    <div>
        <asp:Label ID="Answer" runat="server" Text="找回密碼答案:"></asp:Label>
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></div>
    <div >
        <asp:Label ID="Email" runat="server" Text="電子郵件:"></asp:Label>
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></div>
    <div >
        <asp:Label ID="Label1" runat="server" Text="驗證碼:"></asp:Label>
        <asp:TextBox ID="CheckCode" runat="server" Width="85px"></asp:TextBox>
        <asp:Image id="Image1" runat="server" ImageUrl="Validate.aspx"></asp:Image></div>
    <div>
        <asp:Button ID="Button1" runat="server" Text="確定" /></div>
    <div class="text">
        <asp:Label ID="Message" runat="server" ></asp:Label></div>
    </div>
    </form>
</body>
</html>
-------------------------register.aspx.cs-------------------

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;
namespace MyRegister
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string checkcode = CheckCode.Text;
            //Response.Write(Session["VNum"]);
            if (checkcode == Session["VNum"].ToString() || Session["VNum"].ToString()==null)//注意Session["VNum"].ToString(),必須加上ToString(),因//為Session["VNum"]是對象。
            Response.Redirect ("default.aspx");
            else
            Message.Text = "驗證碼錯誤或為空白!";
        }
    }
}
---------------------------------

其中注釋的地方應特別注意



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。