ASP.net implementation Verification Code technology (online collection)

Source: Internet
Author: User
Tags empty tostring trim
Asp.net| Verification Code

The first step is to generate the verification code

Add a validatecode.aspx page

The code in ValidateCode.aspx.cs is as follows

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;
Using System.Drawing;
Using System.Drawing.Imaging;

public partial class ValidateCode:System.Web.UI.Page
{//This page will be used to generate the CAPTCHA picture
protected void Page_Load (object sender, EventArgs e)
{//Calling function to generate a picture of a captcha
This. Createcheckcodeimage (Generatecheckcode ());

}
private String Generatecheckcode ()
{//Generate five-bit random string
int number;
char code;
string checkcode = String.Empty;

System.Random Random = new Random ();

for (int i = 0; i < 5; i++)
{
Number = random. Next ();

if (number% 2 = 0)
Code = (char) (' 0 ' + (char) (number% 10));
Else
Code = (char) (' A ' + (char) (number% 26);

Checkcode + = code. ToString ();
}

RESPONSE.COOKIES.ADD (New HttpCookie ("Checkcode", Checkcode));
session["Checkcode"] = checkcode;//for client parity code comparisons

return checkcode;
}

private void Createcheckcodeimage (string checkcode)
{///generate a picture display for the verification code
if (Checkcode = null | | Checkcode.trim () = String.Empty)
Return

System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling (Checkcode.length * 12.5)), 22;
Graphics g = graphics.fromimage (image);

Try
{
Generate a random generator
Random Random = new Random ();

Empty picture background color
G.clear (Color.White);

           //background noise line for picture
             for (int i = 0; i < i++)
             {
                 int x1 = random. Next (image. Width);
                int x2 = random. Next (image. Width);
                int y1 = random. Next (image. Height);
                int y2 = random. Next (image. Height);

G.drawline (New Pen (color.silver), x1, y1, x2, y2);
}

Font font = new System.Drawing.Font (Arial), (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush (New Rectangle (0, 0, image.) Width, image. Height), Color.Blue, color.darkred, 1.2f, true);
g.DrawString (Checkcode, Font, brush, 2, 2);

Picture of the foreground noise point
for (int i = 0; i < i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);

Image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));
}

           //Picture border lines
             G.drawrectangle (New Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1);

System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
}

Second Step Test verification code

Establish test.aspx

Form id= "Form1" runat= "Server" >
<div>

<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "Server" text= "button"/><br

<asp:image id= "Image1" runat= "Server" imageurl= "~/validatecode.aspx"/>//the picture will be used to display the verification code
</div>
</form>

Test.aspx.cs Code

protected void Page_Load (object sender, EventArgs e)
{

}
protected void Button1_Click (object sender, EventArgs e)
{

if (this. TextBox1.Text.ToString (). Trim () = = session["Checkcode"]. ToString ()) {Response.Write ("<script lauguage= ' JavaScript ' >alert (' verify success ');</script>");}
}



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.