Create and verify the verification code when logging on to asp.net, and verify the asp.net verification code.

Source: Internet
Author: User

Create and verify the verification code when logging on to asp.net, and verify the asp.net verification code.

1. Create a page, ImageCode. aspx

2. Add the following code to Page_Load:

String tmp = RndNum (4 );
HttpCookie a = new HttpCookie ("ImageV", tmp );
Response. Cookies. Add ();
This. ValidateCode (tmp );

3. Add two methods to the 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 a random Generator

Random random = new Random ();

// Background color
G. Clear (Color. White );

For (int I = 0; I <100; 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 = ", 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 an image control on the page to display the verification code.

5. Add the following code on the front-end 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 one </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 verification code is correct:

String code = tbx_yzm.Text;
HttpCookie htco = Request. Cookies ["ImageV"];
String scode = htco. Value. ToString ();
If (code! = Scode)
{
Response. Write ("<script> alert ('incorrect verification code! ') </Script> ");
}

8. Results

Related Article

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.