Implementation of the Asp.net 2.0 verification module

Source: Internet
Author: User
1. Build a module and compile it into a DLL.

Namespace aspnetajaxweb. validatecode. Page
{
Public class validatecode: system. Web. UI. Page
{
Private const double imagelengthbase= 12.5;
Private const int imageheigth = 22;
Private const int imagelinenumber = 25;
Private const int imagepointnumber = 100;
Public static string validatecodekey = "validatecodekey ";

Private int length = 4;
Private string code = string. empty;

/// <Summary>
/// Obtain or set the verification code length. The default value is 4.
/// </Summary>
Public int Length
{
Get
{
Return length;
}
Set
{
Length = value;
}
}

/// <Summary>
/// Obtain the verification code
/// </Summary>
Public String code
{
Get
{
Return code;
}
}

Public validatecode ()
{

}

Protected override void onload (eventargs E)
{
Createvalidateimage (length );
}

///


// create a random Verification Code
///
/// Verification code length
//
Public String createcode (INT length)
{< br> If (length <= 0) return string. empty;
// create a random number and construct the Verification Code
random = new random ();
stringbuilder sbcode = new stringbuilder ();
for (INT I = 0; I {< br> sbcode. append (random. next ();
}< br> // Save the verification code to the session object
code = sbcode. tostring ();
session [validatecodekey] = Code;
return code;
}

///


// create an image and Verification Code
///
/// Verification code length
Public void createvalidateimage (INT length)
{// create a Verification Code
code = createcode (length);
// create an image of the Verification Code
createvalidateimage (CODE );
}

/// <Summary>
/// Image and verification code for creating the verification code
/// </Summary>
/// <Param name = "code"> Verification Code </param>
Public void createvalidateimage (string code)
{
If (string. isnullorempty (CODE) = true) return;
/// Save the verification code to the Session Object
Session [validatecodekey] = code;
/// Create an image
Bitmap image = new Bitmap (INT) math. Ceiling (code. length * imagelengthbase), imageheigth );
Graphics G = graphics. fromimage (image );

/// Random number generator
Random random = new random ();

Try
{
/// Clear the image and specify the fill color
G. Clear (color. White );

// draw the interference line of the image
int x1, x2, Y1, Y2;
for (INT I = 0; I {< br> X1 = random. next (image. width);
Y1 = random. next (image. height);
X2 = random. next (image. width);
Y2 = random. next (image. height);
// draw the interference line
G. drawline (new pen (color. silver), X1, Y1, X2, Y2);
}

/// Draw the verification code
Font font = new font ("tahoma", 12, fontstyle. Bold | fontstyle. italic );
Lineargradientbrush brush = new lineargradientbrush (New rectangle (0, 0, image. Width, image. Height ),
Color. Blue, color. darkred, 1.2f, true );
G. drawstring (Code, Font, brush, 2.0f, 2.0f );

/// Foreground noise of the image
Int X, Y;
For (INT I = 0; I <imagepointnumber; I ++)
{
X = random. Next (image. width );
Y = random. Next (image. Height );
/// Draw point
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 );
/// Save the image content to the stream
Memorystream MS = new memorystream ();
Image. Save (MS, imageformat. GIF );
/// Output image
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (Ms. toarray ());
}
Finally
{// Release occupied Resources
G. Dispose ();
Image. Dispose ();
}
}
}
}

2. Create a validatecode. ASP page.
<% @ Page Language = "C #" autoeventwireup = "false" inherits = "aspnetajaxweb. validatecode. Page. validatecode" %>
3. reference the compiled DLL where the verification code is to be used.
4. areas to use
<Tr bgcolor = "white">
<TD> Verification Code: </TD>
<TD>
<Asp: textbox id = "tbcode" runat = "server" skinid = "tbskin" width = "80px"> </ASP: textbox>
<Asp: Image id = "imgcode" runat = "server" imageurl = "~ /Validatecode. aspx "/>
</TD>
</Tr>
5 Logic Code
// Determine whether verification is created
If (session [validatecode. validatecodekey] = NULL) return;
/// Verify that the verification code is equal
If (tbcode. Text! = Session [validatecode. validatecodekey]. tostring ())
{
Lbmessage. Text = "Incorrect verification code. Please enter it again. ";
Return;
}

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.