Implement the verification code for numbers and characters under ASP.NET

xiaoxiao2021-03-06  82

I often see how to ask how to implement the verification code, in fact, there are many articles on the verification code in the 9CBS, DEV-Club, etc., but many articles only spend how to output a random generated number or character. The image, of course, this is the core of the verification code, but for many ASP.NET's beginners, how to use the image it generated again (forum has a lot of ask this), this is also I wrote a reason for this article.

For the reason, I will not say much about the principle of verification code. You can see other articles. The end of the article is accompanied by a complete example code. There is a detailed note, you can skip the explanation of words, use directly

First, I want to briefly talk about the usage of session and viewstate, because it will be used later.

Store data in session: Session ("key") = "test"

Take the value from the session: DIM TestValue As String = Session ("Key")

akin:

Store data in ViewState: ViewState ("Key") = "TEST"

Take the value from ViewState: DIM TestValue As String = ViewState ("Key")

About ViewState's more detailed information, you can see the "ASP.NET ViewState for MSDN"

It's better to see it, sometimes the code itself is more expressive than any explanation, so there is no more code to explain too much. The verification code implemented by this article needs to use two files:

GIF.ASPX This file is used to generate a verification code

Validatecode.aspx This file is used to test the verification code (ie how to use)

Here is the full code of GIF.ASPX:

<% @ Import namespace = "system"%>

<% @ Import namespace = "system.io"%>

<% @ Import namespace = "system.drawing"%>

<% @ Import namespace = "system.drawing.imaging"%>