I often see posts on the forum asking how to implement the verification code. In fact, there are many articles about the verification code on the website, however, many articles only talk about how to output a random number or character image. Of course, this is the core of the verification code, but for many ASP. net beginners, how to use the image generated by it has become a problem (many questions in the Forum), which is also one of the reasons I wrote this article.
Let's get down to the truth. I will not talk much about the principles of the Verification Code. For more information, see other articles. At the end of this article, we will provide a complete example code with detailed annotations. you can skip the explanatory text and use it directly.
First, I want to briefly talk about the usage of session and viewstate, because it will be used later.
Store data in the session: Session ("key") = "test"
Value from session: dim testvalue as string = SESSION ("key ")
Similar:
Store data in viewstate: viewstate ("key") = "test"
Value From viewstate: dim testvalue as string = viewstate ("key ")
Sometimes the Code itself is more expressive than any explanation, so there is not much explanation for the code here. The Verification Code implemented in this article requires two files:
GIF. aspx this file is used to generate a verification code.
Validatecode. aspx this file is used to test the verification code (that is, how to use it)
The complete GIF. aspx code is provided below:
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Drawing" %>
<% @ Import namespace = "system. Drawing. Imaging" %>
<Script language = "VB" runat = "server">
Sub page_load (sender as object, e as eventargs)
'Rndnum is a UDF.
Dim vnum as string = rndnum (4)
Session ("vnum") = vnum
Validatecode (vnum)
End sub
'Image verification code generation function
Sub validatecode (vnum)
Dim IMG as system. Drawing. Bitmap
Dim G as graphics
Dim MS as memorystream
Dim gheight as integer = int (LEN (vnum) * 11.5)
'Gheight indicates the image width. The image width is automatically changed based on the Character length.
IMG = new Bitmap (gheight, 20)
G = graphics. fromimage (IMG)
G. drawstring (vnum, (new font ("Arial", 10), (new solidbrush (color. blue), 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 (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"
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>
The following code shows how to use the image verification code generated by the file:
<Asp: Image id = "image1" runat = "server" imageurl = "GIF. aspx"/>
This is the image control used to display the verification code. You can place it wherever you like. The following provides detailed code for use. You can save it as validatecode. aspx and associate it with GIF. put aspx in the same directory and open validatecode in the browser. aspx, you can test its effect:
<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 and can be changed as needed.
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
Lblshow. Text = "<font color = 'red'> tip: Pass verification </font>"
Else
Lblshow. Text = "The entered verification code is inconsistent with the given verification code"
End if
End sub
</SCRIPT>
<HTML>
<Body>
<Form runat = "server">
<Div align = "center">
& Lt; table width = "750" & gt;
<! -- Dwlayouttable -->
<Tr>
<TD width = "256" Height = "46"> & nbsp; </TD>
<TD width = "9"> & nbsp; </TD>
<TD width = "88"> & nbsp; </TD>
<TD width = "87"> & nbsp; </TD>
<TD width = "100"> & nbsp; </TD>
<TD width = "68"> & nbsp; </TD>
<TD width = "97"> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "21"> </TD>
<TD> </TD>
<TD colspan = "3" valign = "TOP"> <asp: Label id = "lblshow" runat = "server"> </ASP: Label> </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "14"> </TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</Tr>
<Tr>
<TD Height = "21"> & nbsp; </TD>
<TD colspan = "2" valign = "Middle"> Verification Code: </TD>
<TD valign = "TOP"> <asp: Image id = "image1" runat = "server" imageurl = "GIF. aspx"/> </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "20"> & nbsp; </TD>
<TD colspan = "2" valign = "TOP"> enter the verification code: </TD>
<TD valign = "TOP"> <asp: textbox id = "txtvalidatecode" runat = "server" textmode = "singleline"/> </TD>
<TD colspan = "2" valign = "Middle"> <font color = "# ff0000" size = "2"> * Note: Case Sensitive </font> </TD>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "25"> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD Height = "19"> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD valign = "TOP"> <asp: button id = "btnsubmit" runat = "server" text = "Compare" onclick = "btnsubmit_click"/> </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
<TD> & nbsp; </TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>