Skill-custom "Verification Code" Control
"Verification Code" is often used during login. Is it nice to encapsulate "she" as a control? However, I generally do not like the "she" that is encapsulated so badly, because I can't see anything, or it may seem awkward :)
Entry Point
Use the ihttphandler interface to dynamically generate verification Codes
Figure 1 (view the results directly)
ViewCodeRight
Step 1 (create a class-1 Database)
Code
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. drawing;
Namespace controlvalidater
{
Public class cvalidater: ihttphandler, irequiressessionstate
{
Httpresponse response = NULL;
Httpsessionstate sessionstate = NULL;
Public void processrequest (httpcontext context)
{
Response = context. response;
Sessionstate = context. Session;
This. genimg (this. gencode (4 ));
}
Bool system. Web. ihttphandler. isreusable
{
Get {return true ;}
}
// Generate a random string
Private string gencode (INT num)
{
String [] source = {"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", "V", "W", "X ", "Y", "Z "};
String code = "";
Random RD = new random ();
For (INT I = 0; I <num; I ++)
{
Code + = source [RD. Next (0, source. Length)];
}
Return code;
}
// Generate an image
Private void genimg (string code)
{
Bitmap mypalette = new Bitmap (60, 20); // defines a canvas
Graphics GH = graphics. fromimage (mypalette); // an instance of drawing defined on the drawing board
Rectangle rc = new rectangle (0, 0, 60, 20); // defines a rectangle.
Gh. fillrectangle (New solidbrush (color. Silver), RC); // fill the rectangle
Gh. drawstring (Code, new font ("", 16), new solidbrush (color. Gray), RC); // draw a string in the rectangle
Mypalette. Save (response. outputstream, system. Drawing. imaging. imageformat. JPEG); // display the image
Sessionstate ["validatecode"] = Code; // Save the string to the session for verification as needed
Gh. Dispose ();
Mypalette. Dispose ();
}
}
}
Step 2 (usage)
Directly reference the compiled DLL file
Add the For example
Code
<Httphandlers>
<Add Path = "cvalidater. aspx" verb = "*" type = "controlvalidater. cvalidater"/>
</Httphandlers>
Of course, if you directly use the class library as a common class and put it in the app_data directory of the website in use, you do not need to configure it :)
step 3 (usage)
On the page where the verification code is required, place an Image Tag and point its URL to "class name. aspx "
example:
Code
is the configuration complete now? :)
ah...
paste the code for the last step.
Code
protected void button#click (Object sender, eventargs e)
{< br> string strin = This. textbox1.text;
string strsess = session ["validatecode"]. tostring ();
If (strin = strsess)
button1.text = "OK";
else
button1.text = "error ";
}< br> Note:
1. Remember to implement the irequiressessionstate interface. Although it is only a tag, the session status cannot be used without her.
2. The httpsessionstate object is obtained through context.
3. Do not forget Path = "cvalidater. aspx ", if Path = "*. aspx ", then all your pages will be processed by cvalidater. This is not what we want.