VB.net implements the verification code Function

Source: Internet
Author: User

Implementation process: On the page to be called, use the image control to call the page generated by the Verification Code. On the page generated by the Verification Code, generate a verification image and save the verification information to the session, the call page uses session to determine whether the user input is correct. The reason is that the following code is used:
  
Verification code generation page:
GIF. aspx
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Drawing" %>
<% @ Import namespace = "system. Drawing. Imaging" %>
  
<Script language = "VB" runat = "server">
Sub page_load (byval sender as object, byval e as eventargs)
'Rndnum is a function for generating random codes,
Dim vnum as string = rndnum (4) 'this value is the number of digits that generate the verification code
Session ("vnum") = vnum' read session
Validatecode (vnum) 'generates an image based on the session
End sub
  
'--------------------------------------------
'Image verification code generation function
Sub validatecode (byval vnum)
Dim IMG as system. Drawing. Bitmap
Dim G as graphics
Dim MS as memorystream
Dim gheight as integer = int (LEN (vnum) * 14)
'Gheight indicates the image width. The image width is automatically changed based on the Character length.
IMG = new Bitmap (gheight, 24)
G = graphics. fromimage (IMG)
G. drawstring (vnum, (new font ("Arial", 12), (new solidbrush (color. red), 3, 3) 'draw the string (string, Font, paint color, top left X in the rectangle. Y on the top left)
MS = new memorystream ()
IMG. Save (MS, imageformat. PNG)
Response. clearcontent () 'needs to output image information to modify the HTTP Header
Response. contenttype = "image/PNG"
Response. binarywrite (Ms. toarray ())
G. Dispose ()
IMG. Dispose ()
Response. End ()
End sub
  
'--------------------------------------------
'Function name: rndnum
'Function parameter: vcodenum -- set the number of digits of the returned random string
'Function: generate a random string mixed with numbers and characters
Function rndnum (byval vcodenum)
Dim vchar as string = ", A, B, C, D, E, F, G, H, I, J, K, L, m, N, O, P, Q, R, S, T, U, w, x, y, z "'needs to be verified in Chinese. You can modify the font here and in the validatecode function.
Dim vcarray () as string = Split (vchar, ",") 'generates an array of strings
Dim vnum as string = ""
Dim I as byte
For I = 1 to vcodenum
Randomize ()
Vnum = vnum & vcarray (INT (35 * RND () 'array is generally read from 0, so here it is 35 * RND
Next
Return vnum
End Function
</SCRIPT>
  
  
Call page:
Demo. aspx
<Script language = "VB" runat = "server">
Sub page_load (sender as object, e as eventargs)
Dim vnum as string = SESSION ("vnum ")
Session. Abandon ()
Viewstate ("vnum") = vnum
End sub
'The following Event code is used to test the verification code
  
Sub btnsubmit_click (sender as object, e as eventargs)
'Determine whether the entered verification code is the same as the given Verification Code
If txtvalidatecode. Text = CSTR (viewstate ("vnum") then
Ensonshow. Text = "<font color = 'red'> prompt: verification passed </font>"
Else
Ensonshow. Text = "The entered Verification Code does not match the given verification code"
End if
End sub
  
</SCRIPT>
<HTML>
<Body>
<Form runat = "server">
<Div align = "center">
& Lt; table width = "750" & gt;
<Tr>
<TD valign = "Middle"> Verification Code: </TD>
<! -- Call the verification image generation page --->
<TD valign = "TOP"> <asp: Image id = "image1" runat = "server" imageurl = "GIF. aspx"/> </TD>
</Tr>
<Tr>
<TD valign = "TOP"> enter the verification code: </TD>
<TD valign = "TOP"> <asp: textbox id = "txtvalidatecode" runat = "server" textmode = "singleline"/> <font color = "# ff0000" size = "2"> * case sensitive </font> </TD>
</Tr>
<Tr>
<TD colspan = "2" valign = "TOP" align = "center"> <asp: button id = "btnsubmit" runat = "server" text = "verify" onclick = "btnsubmit_click"/>
<Asp: Label id = "ensonshow" runat = "server"/>
</TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>

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.