ASP. NET -- production of the Verification Code, asp.net Verification Code

Source: Internet
Author: User

ASP. NET -- production of the Verification Code, asp.net Verification Code

We often encounter a verification code entry when logging on to the website, posting a blog or submitting a comment. At that time, we finally got in touch with niu Yi's news publishing system, here is a summary.

There are three things used: An aspx page and an image.

First, create An aspx page.

<% @ WebHandler Language = "C #" Class = "Picture" %> using System; using System. web; using System. drawing; using System. drawing. drawing2D; using System. web. sessionState; public class Picture: IHttpHandler, IRequiresSessionState // You must implement this interface to use session. Remember to import System. web. sessionState namespace {public void ProcessRequest (HttpContext context) {string checkCode = GenCode (5); // generates a five-character random context. session ["Code"] = checkCode; // Save the string to the Session so that System can be verified as needed. drawing. bitmap image = new System. drawing. bitmap (70, 22); Graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background color g. clear (Color. white); // specifies the background noise line of the image. int I; for (I = 0; I <25; 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", 12, (System. drawing. fontStyle. bold); 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); // The foreground noise of the picture. 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); context. response. clearContent (); context. response. contentType = "image/Gif"; context. response. binaryWrite (ms. toArray ();} finally {g. dispose (); image. dispose ();}} /// <summary> /// generate a random string /// </summary> /// <param name = "num"> random output of several characters </param> // /<returns> random string </returns> private string GenCode (int num) {string str = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char [] chastr = str. toCharArray (); string code = ""; Random rd = new Random (); int I; for (I = 0; I <num; I ++) {// code + = source [rd. next (0, source. length)]; code + = str. substring (rd. next (0, str. length), 1) ;}return code ;}public bool IsReusable {get {return false ;}}}
Next, add an img control on the interface that uses the verification code, and the src property points to the. ashx file.

 

Then, determine whether the submitted verification code is correct.

// Determine whether the verification code is entered correctly string code = txtCode. text. trim (). toUpper (); // convert all input letters to uppercase and then compare them to string rightCode = Session ["Code"]. toString (); if (code! = RightCode) {Page. ClientScript. RegisterStartupScript (Page. GetType (), "message", "<script language = 'javascript 'defer> alert ('verification code input error! '); </Script> "); // The system prompts an error to ensure that the background remains unchanged. return ;}

Finally, use the commit crip to refresh the verification code. Because there are few codes, they can be nested in the code.

<Asp: Content ID = "Content1" ContentPlaceHolderID = "head" Runat = "Server"> <script language = "javascript" type = "text/javascript"> function changeCode () {var imgNode = document. getElementById ("vimg"); imgNode. src = "handler/WaterMark. ashx? T = "+ (new Date ()). valueOf (); // here, a time parameter is added to prevent browser cache problems.} </script> </asp: Content>


Create an ASPNET Verification Code

Display the verification code page (front-end)

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "AdminLogin. aspx. cs" Inherits = "Admin_AdminLogin" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head runat = "server">
<Title> Admin Logon </title>
<Script type = "text/javascript">
Function changeimg (){
Var myimg = document. getElementById ("img_Check ");
Now = new Date (); // if this event is not set, a bug can only be changed once.
Myimg. src = "AdminImage. aspx? Code = "+ now. getTime ();
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div align = "center">
<Table>
<Tr>
<Td align = "right">
Verification Code: <asp: TextBox ID = "tb_Check" runat = "server" Width = "70"> </asp: TextBox>
<Asp: Image ID = "img_Check" runat = "server" ImageUrl = "~ /Admin/AdminImage. aspx "Vis... the remaining full text>

How can I create an ASPNET verification code?

Webzixue.javaeye.com/blog/475841
 

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.