Asp.net| Verification Code
Often see in the forum to ask how to implement verification code of the post, in fact, the article on the verification Code on the site have a lot, but many articles only tell how to output a random generation of numbers or characters of the image, of course, this is the core of the verification Code, But for many asp.net beginners, how to use its generated image has become a problem (the forum has a lot to ask this), which is one of the reasons I write this article.
In the end, on the principle of verification code, I will not say more, we can see other articles, with complete example code, there are detailed comments, you can skip the interpretation text, direct use.
First of all, I would like to briefly talk about the use of session and ViewState, because it will be used later.
Store data in session: Session ("key") = "Test"
Value from session: Dim testvalue As String=session ("key")
Similar to:
Store data in ViewState: ViewState ("key") = "Test"
Value from ViewState: Dim testvalue As String=viewstate ("key")
Believing, sometimes the code itself is more expressive than any commentary, so there is no more code commentary here, the verification code implemented in this article requires two files:
Gif.aspx This file is used to generate the verification code.
Validatecode.aspx This file is used to test the verification code (that is, how to use)
The complete code for gif.aspx is given 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 custom function
Dim vnum As String=rndnum (4)
Session ("Vnum") =vnum
Validatecode (Vnum)
End Sub
' Generate an Image verification code 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 for picture width, automatically change picture width based on character length
Img=new BitMap (gheight,20)
G=graphics.fromimage (IMG)
g.DrawString (Vnum, (New Font ("Arial")), (New SolidBrush (Color.Blue), 3, 3) ' Draws a string (string, font, brush color, upper left X.) in the rectangle.
Ms=new MemoryStream ()
Img. Save (Ms,imageformat.png)
Response.clearcontent () ' needs to output image information to modify HTTP headers
Response.contenttype= "Image/png"
Response.BinaryWrite (Ms. ToArray ())
G.dispose ()
Img. Dispose ()
Response.End ()
End Sub
’--------------------------------------------
' Function name: rndnum
' function parameter: vcodenum--sets the number of digits to return a random string
' function function: a random string that produces a mixture of numbers and characters
Function Rndnum (Vcodenum)
Dim Vchar As String= "0,1,2,3,4,5,6,7,8,9,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)) ' arrays are generally read from 0, so here's the 35*rnd
Next
Return Vnum
End Function
</script>
Then how to use the file generated by the image verification code, look at this code:
This is the image control used to display the verification code, you can put it in any place you like, the following gives the detailed use of code, You save it as a validatecode.aspx and put it in the same directory as gif.aspx, open validatecode.aspx in the browser, and you can test its effect:
Sub btnSubmit_Click (sender as object,e as EventArgs)
' Determines whether the input verification code is the same as the given
If Txtvalidatecode.text=cstr (ViewState ("Vnum")) Then
lblshow.text= " "
Else
Lblshow.text= "The verification code completed is not in conformity with the given"
End If
End Sub
</script>
<body>
<form runat= "Server" >
<div align= "Center"
<table width= "750" >
!--dwlayouttable-->
<tr>
<TD width= "256" height= ""
<TD width= "9" > </td>
<TD width= "The" "", "</td>
<TD width= "The" "" "</td>
<TD width= "M" > </td>
<TD width= "", "</td>
<TD width= "The" "The" "" "" </td>
</tr>
<tr>
<TD height= "" "> </td>
<td> </td>
<TD colspan= "3" valign= "Top" ><asp:label id= "lblshow" runat= "server" > </asp:label> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<TD height= ">" </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<TD height= "" "> </td>
<TD colspan= "2" valign= "middle" > code: </td>
<TD valign= "Top" ><asp:image id= "Image1" runat= "Server" imageurl= "gif.aspx"/> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<TD height= ">" > </td>
<TD colspan= "2" valign= "Top" Enter CAPTCHA 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> </td>
</tr>
<tr>
<TD height= "" "> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<TD height= "" "> </td>
<td> </td>
<td> </td>
<TD valign= "Top" ><asp:button id= "btnsubmit" runat= "server" text= "compare"/> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>