C # refresh the verification code on the logon page

Source: Internet
Author: User

PNG. aspx

Using system; using system. collections. generic; using system. drawing; using system. web; using system. web. ui; using system. web. UI. webcontrols; namespace maticsoft. web. admin {public partial class PNG: system. web. UI. page {protected void page_load (Object sender, eventargs e) {string checkcode = createrandomcode (4); Session ["checkcode"] = checkcode; createimage (checkcode );} private string createrandomcode (INT codecount) {// function: generate a random string allchar = "0123456789 characters"; char [] allchararray = allchar. tochararray (); string randomcode = ""; random Rand = new random (); For (INT I = 0; I <codecount; I ++) {int r = Rand. next (61); randomcode + = allchararray. getvalue (r);} return randomcode;} private void createimage (string checkcode) {// function for generating image Verification Code int iwidth = (INT) (checkcode. length * 11.5); system. drawing. bitmap image = new system. drawing. bitmap (iwidth, 20); graphics G = graphics. fromimage (image); font F = new system. drawing. font ("Arial", 10, system. drawing. fontstyle. bold); brush B = new system. drawing. solidbrush (color. azure); // letter white // G. fillrectangle (new system. drawing. solidbrush (color. blue), 0, 0, image. width, image. height); G. clear (color. brown); // background gray G. drawstring (checkcode, F, B, 3, 3); pen blackpen = new pen (color. black, 0); random Rand = new random (); system. io. memorystream MS = new system. io. memorystream (); image. save (MS, system. drawing. imaging. imageformat. JPEG); response. clearcontent (); response. contenttype = "image/JPEG"; response. binarywrite (Ms. toarray (); G. dispose (); image. dispose ();}}}

 

Add images to the login page

<Asp: Image id = "image1" runat = "server" imageurl = "PNG. aspx" onclick = "Refresh (this, 'png. aspx ');"/>

JS script

Function refresh ()
{
VaR url = "PNG. aspx? Id = ";
VaR r = math. Random () * 1000;
Url = URL + R;
Document. All ("image1"). src = URL;
Document. All ("loi_txtvc"). value = "";
Document. All ("loi_txtvc"). Focus ();
Return false;
} </SCRIPT>

 

The verification code textbox3 is the verification code input box.

If (base. session ["checkcode"] = NULL) {page. clientscript. registerstartupscript (page. clientscript. getType (), "myscripts", "<SCRIPT> alert (\" Verification Code expired! \ "); </SCRIPT>"); return;} If (textbox3.text! = Session ["checkcode"]. tostring () {page. clientscript. registerstartupscript (page. clientscript. getType (), "myscripts", "<SCRIPT> alert (\" Incorrect verification code! \ "); </SCRIPT>"); return ;}

 

C # refresh the verification code on the logon page

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.