Production and verification of verification code in ASP.

Source: Internet
Author: User

1. Create a new page, imagecode.aspx

2. Add the following code to the Page_Load

string tmp = Rndnum (4);
HttpCookie a = new HttpCookie ("Imagev", TMP);
Response.Cookies.Add (a);
This. Validatecode (TMP);

3. Add two methods to this page

private void Validatecode (String vnum)
{

Bitmap Img = null;

Graphics g = null;

MemoryStream ms = NULL;

int gheight = vnum.length * 12;

IMG = new Bitmap (gheight, 25);

g = Graphics.fromimage (IMG);

Generate Random Generators

Random random = new random ();

Background color
G.clear (Color.White);

for (int i = 0; i <; i++)
{

int x = random. Next (Img.width);

int y = random. Next (Img.height);

Img.setpixel (x, Y, Color.FromArgb (random. Next ()));

}


Text font
Font f = new Font ("Arial Black", 12);

Text color
SolidBrush s = new SolidBrush (Color.Blue);

g.DrawString (Vnum, F, S, 3, 3);

ms = new MemoryStream ();

Img.save (MS, Imageformat.jpeg);

Response.clearcontent ();

Response.ContentType = "Image/jpeg";

Response.BinaryWrite (Ms. ToArray ());

G.dispose ();
Img.dispose ();
Response.End ();

}

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++)
{
if (temp! =-1)
{
Rand = new Random (i * temp * unchecked ((int) DateTime.Now.Ticks));

}
int t = rand. Next (35);
if (temp! =-1 && temp = = t)
{
Return Rndnum (Vcodenum);

}
temp = t;
Vnum + = vcarray[t];

}

return vnum;

}

4. Add a picture control on the page that needs to display the verification code

5. Add the following code to the front page of the page:

Verification Code:
<asp:textbox id= "Tbx_yzm" runat= "Server" width= "70px" ></asp:TextBox>
<asp:imagebutton id= "Ibtn_yzm" runat= "Server"/>
<a href= "Javascript:changecode ()" style= "text-decoration:underline; font-size:10px; " > Change a new </a>
<script type= "Text/javascript" >
function ChangeCode ()
{
document.getElementById (' Ibtn_yzm '). src = document.getElementById (' ibtn_yzm '). src + '? ';
}
</script>

6. Add the following code to the Page_Load event on this page:

Ibtn_yzm. IMAGEURL = "imagecode.aspx";

7. Verify that the code is entered correctly:

String code = Tbx_yzm. Text;
HttpCookie Htco = request.cookies["Imagev"];
String scode = Htco. Value.tostring ();
if (Code! = SCODE)
{
Response.Write ("<script>alert (' CAPTCHA input is incorrect! ') </script> ");
}

8. Effects

Production and verification of verification code in ASP.

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.