The verification code of the combination of digital and character story under ASP.net

Source: Internet
Author: User
Asp.net| verification code is often seen in the forum to ask how to implement the verification code of the post, in fact, the article on the verification Code in Csdn,dev-club and other sites have a lot, but a lot of 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, you can see other articles, with complete example code, there are detailed comments, you can skip the explanatory text, direct use
First, 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")
For more detailed information on ViewState, you can refer to MSDN's <<asp.net ViewState preliminary >>
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:
<asp:image id= "Image1" runat= "Server" imageurl= "gif.aspx"/>
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 validatecode.aspx, and put it and gif.aspx in the same directory, You can test the effect by opening the validatecode.aspx in the browser:
<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 captcha and can be changed as needed
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= "<font color= ' red ' > Hint: Validation through </font>"
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>
&LT;TD width= "256" height= "> </td>"
&LT;TD width= "9" > </td>
&LT;TD width= > </td>
&LT;TD width= > </td>
&LT;TD width= > </td>
&LT;TD width= > </td>
&LT;TD width= > </td>
</tr>
<tr>
&LT;TD height= "></td>"
<td></td>
&LT;TD colspan= "3" valign= "Top" ><asp:label id= "lblshow" runat= "Server" ></asp:label></td>
<td> </td>
<td> </td>
</tr>
<tr>
&LT;TD height= "></td>"
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
&LT;TD height= > </td>
&LT;TD colspan= "2" valign= "middle" > Verification Code:</td>
&LT;TD valign= "Top" ><asp:image id= "Image1" runat= "Server" imageurl= "gif.aspx"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
&LT;TD height= > </td>
&LT;TD colspan= "2" valign= "top" > Input Verification Code:</td>
&LT;TD valign= "Top" ><asp:textbox id= "Txtvalidatecode" runat= "Server" textmode= "Singleline"/></td>
&LT;TD colspan= "2" valign= "Middle" ><font color= "#FF0000" size= "2" >* Note: case-sensitive </font></td>
<td> </td>
</tr>
<tr>
&LT;TD height= > </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
&LT;TD height= "> </td>"
<td> </td>
<td> </td>
&LT;TD valign= "Top" ><asp:button id= "btnsubmit" runat= "server" text= "compare" onclick= "btnSubmit_Click"/></td >
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>


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.