About the source code and problem solving of generating verification code

Source: Internet
Author: User
Tags tostring
Solution | problem | Verification code

In the online search for a verification code source code, after the use of the following problems: Whether you enter the correct or not, you will be prompted to verify the code error. Later, after the QQ group and CSDN Post Consulting, learned that is the Internet Explorer cache problem. Corrected the error after being pointed by others. The source code is as follows:

-------------------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; Special attention
Response.Cache.SetExpires (DateTime.Now.AddMilliseconds (-1));//Special attention
Response.Cache.SetCacheability (Httpcacheability.nocache)/special attention
Response.appendheader ("Pragma", "No-cache"); Special attention
String Vnum =makevalidatecode ();
session["Vnum"]=vnum;//obtain the verification code for later verification
Validatecode (vnum);}
public void Validatecode (string vnum)
{
Validateimage = new Bitmap (A, PIXELFORMAT.FORMAT24BPPRGB);
g = Graphics.fromimage (validateimage);
G.fillrectangle (New LinearGradientBrush (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", one), 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">

<title> Member Registration </title>
<link href= "Stylesheet.css" _fcksavedurl= "Stylesheet.css" "_fcksavedurl=" "Stylesheet.css" "_fcksavedurl=" " Stylesheet.css "" "Type=" Text/css "rel=" stylesheet ">
<body>
<form id= "Form1" runat= "Server" >
<div class= "Middle" style= "height:1px" >
<div >
<asp:label id= "UserName" runat= "server" text= "Username:" ></asp:Label>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
</div>
<div >
<asp:label id= "USERPSW" runat= "server" text= "Password:" ></asp:Label>
<asp:textbox id= "TextBox2" runat= "Server" ></asp:TextBox></div>
<div >
<asp:label id= "Label2" runat= "Server" text= "Confirm Password:" ></asp:Label>
<asp:textbox id= "TextBox6" runat= "Server" ></asp:TextBox></div>
<div >
<asp:label id= "question" runat= "Server" text= "Retrieve password question:" ></asp:Label>
<asp:textbox id= "TextBox3" runat= "Server" ></asp:TextBox></div>
<div>
<asp:label id= "Answer" runat= "Server" text= "Retrieve password answer:" ></asp:Label>
<asp:textbox id= "TextBox4" runat= "Server" ></asp:TextBox></div>
<div >
<asp:label id= "Email" runat= "server" text= "Email:" ></asp:Label>
<asp:textbox id= "TEXTBOX5" runat= "Server" ></asp:TextBox></div>
<div >
<asp:label id= "Label1" runat= "Server" text= "Authenticode:" ></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= "OK"/></div>
<div class= "Text" >
<asp:label id= "message" runat= "Server" ></asp:Label></div>
</div>
</form>
</body>
-------------------------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)//Note session["Vnum"]. ToString (), must add ToString (), because//is session["Vnum"] is an object.
Response.Redirect ("default.aspx");
Else
Message.Text = "Authentication code error or empty!" ";
}
}
}
---------------------------------

Where notes should be paid special attention to



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.