An ASP. NET Verification Code class

Source: Internet
Author: User
The first reflection is whether these properties of the CAPTCHA can be set (that is, encapsulated into a class and then used in a way that exposes public properties and methods, or is implemented directly in the generic handler to generate the verification code to the output), the result is a pity, the latter ... Inside the algorithm feel pretty good, at least for me this algorithm is almost not understand the people, so think to encapsulate her and then shamelessly into their own class library ^ ^

First analyze the deficiencies in this code (as you think):
1, process-oriented programming, if you want to modify the verification code properties, such as modifying the font size, background color and other details of things, you need to go to the general processing program to find the relevant code, modify it. However, this code is good, the generation of verification code strings and generated images in different ways, it is easy to find the place to modify.
2, if I want to apply this function to other places, such as the WinForm program, then this code is not done in a component (a DLL) is reused, but is copied and pasted and then modified ...
3, did not find the verification code after the creation of the saved up (--! )

Of course, the so-called inadequacy is relatively, is that there is time to think and then slowly design a verification code class will certainly be able to make a relatively good class out, but if it is in the project and the project is very urgent, such code is actually compared to force, those so-called object-oriented design pattern code decoupling what is floating clouds ~ ~~

The next step was my revision of the code, first to determine what kind of effect I wanted, that is, how I want to use him when I wrap it up. First of all, I want to use only a new verification code instance (if required can be arbitrary settings of the verification code), and then you can call this instance of some of the methods in the form of strings, streams, bitmap objects, byte arrays to obtain this verification code (description, This verification code class does not include the verification code into the context of the function, the individual feel that the generation of verification code and the Verification code to the context is two different functions, there is no need to do this function in the verification code class to go. In this case, the code in the general processing program becomes very simple, first as long as the new verification code, and then call the method as a string to obtain the verification code, and then save in the afternoon, as in the presence of the session or cookie as needed, the verification code class regardless of these Then another method is called to obtain this verification code in the form of a byte array, through the context. The Response.BinaryWrite () method can output a picture, which means that there are only three lines of code in the general processing program. The code that is called is roughly the following:

<summary>///Get Verification code (general handler entry function)///</summary>//<param name= "Context" > Current context </param> public void ProcessRequest (HttpContext context) {//Create verification code Validatecode Validatecode = new Validatecode ();//Get Verification Code (string), write into session context. session["Somevalidatecode"] = validatecode.getstring (); Output Verification Code (picture) context. Response.BinaryWrite (Validatecode.getbytearray ()); }

PS: Remember just came out of work when the code is poorly written (worse than now), generally get a copy of the code is directly used, never thought about whether to change the code or packaging, then my master was taught a very deep impression. Personal feeling in fact with other people's code is not bad, a person has used at least to prove the availability of code, in addition to limited IQ, some things they want to break the scalp and no one else think good, but with someone else's code must not blindly take the use, the best according to their actual situation to make necessary modifications or encapsulation, Even if it is a simple separate layer. Of course, or that sentence, if the project is urgent to do this is obviously a waste of time, in short, do not think, take the use of, at least see the key code or the overall structure of the code to clarify.

A more encapsulated ASP. NET validation code class related articles please follow topic.alibabacloud.com!

  • Related Article

    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.