Verification code on the logon page

Source: Internet
Author: User
Tags transparent image

When I first added the verification code function to the logon page, I wrote the verification code in the background code on the logon page. Only the verification code is displayed and nothing else is displayed. The verification code is to be written on another page and called.
On the login page, you can write a verification code without writing any other code.
Code of the Login. aspx page
[Csharp]
<Span style = "font-size: 18px;"> <label style = "text-align: right"> Verification Code </label>
<Asp: TextBox style = "width: 50px; float: left; margin-left: 18px"> </asp: TextBox>
<Input class = "text-input" name = "CheckCode" type = "text"
Style = "width: 50px; float: left; margin-left: 18px"/>
Onclick = "this. src = 'checkcode. aspx? Rnd = '+ Math. random (); "/> </span>

The login. aspx. cs Page code will not be written, mainly the login logic code.


The Code on the verification code page is as follows:
The CheckCode. aspx page has no code. A transparent image is required to display the verification code.
CheckCode. aspx. cs code
[Csharp]
<Span style = "font-size: 18px;"> using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Drawing;
Using System. Drawing. Drawing2D;
Using System. Drawing. Imaging;
Using System. IO;
Using System. Collections;
 
Public partial class System_CheckCode: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// Assign the generated verification code string to the code
String code = GenerateCheckCode ();
// Use cookies to save the verification code. The verification code expires in one minute.
HttpCookie CheckCode = new HttpCookie ("CheckCode", code );
CheckCode. Expires = new DateTime (6000 );
Response. AppendCookie (CheckCode );
// Draw an image based on the generated Verification Code
CreatCheckCodeImage (CheckCode. Value );
}
// Generate a verification code string
Private string GenerateCheckCode ()
{
Int number;
Char code;
String checkCode = string. Empty;
Random random = new Random ();
// You can set the number of characters in the verification code.
For (int I = 0; I <4; I ++)
{
// Return a non-negative random number
Number = random. Next ();
 
// Set conditions for generating random numbers
If (number % 2 = 0)
{
Code = (char) ('0' + (char) (number % 10 ));
}
Else
{
Code = (char) ('A' + (char) (number % 26 ));
}
// Complete verification code
CheckCode + = code. ToString ();
}
Return checkCode;
 
}
 
 
// Generate an image based on the input verification code string
Private void CreatCheckCodeImage (string checkCode)
{
If (checkCode = null | checkCode. Trim () = string. Empty) return;
Bitmap image = new Bitmap (int) (Math. Ceiling (checkCode. Length * 17.5), 28 );
 
Graphics g = Graphics. FromImage (image );
Try
{
Random random = new Random ();
// Clear the background color of the image
G. Clear (Color. White );
// Draw the background noise line of the image
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 );
// Draw a line of two points specified by the coordinate pair
G. DrawLine (new Pen (Color. Silver), x1, y1, x2, y2 );
 
}
 
// Set the Font style and use the specified size and style to initialize the new Font.
Font font = new Font ("Arial", 17, (FontStyle. Bold | FontStyle. Italic ));
 
// Use a linear gradient to encapsulate the Brush. This class cannot be inherited.
// Rectangle (Int32, Int32, Int32, Int32) initializes a new instance of the Rectangle class with the specified position and size
LinearGradientBrush brush =
New LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color. Blue, Color. DarkRed, 1.2f, true );
// DrawString (String, Font, Brush, Single, Single)
<Span style = "font-size: 18px;"> // </span> draw the specified text string at the specified position and with the specified Brush and Font object.
G. DrawString (checkCode, font, brush, 2, 2 );
 
// Foreground noise of the image
For (int I = 0; I <100; I ++)
{
Int x = random. Next (image. Width );
Int y = random. Next (image. Height );
// Image. SetPixel (x, y, Color) obtains the Color of the specified pixel in this Bitmap.
// FromArgb (Int32) creates a Color structure from a 32-bit ARGB value.
Image. SetPixel (x, y, Color. FromArgb (random. Next ()));
}
 
// Draw the border line of the image
G. DrawRectangle (new Pen (Color. Silver), 0, 0, image. Width-1, image. Height-1 );
MemoryStream MS = new MemoryStream ();
Image. Save (MS, ImageFormat. Gif );
Response. ClearContent ();
Response. ContentType = "image/Gif ";
Response. BinaryWrite (ms. ToArray ());
 
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
 
}
} </Span>

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.