Encapsulate an asp.net captcha class

  • 2020-05-12 02:26:09
  • OfStack

The first reflection is whether the 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 directly in the 1 type processor to achieve the generation of captcha to the output), the result is more unfortunate, is the latter... The algorithm felt pretty good, at least for someone like me who barely knew algorithms, so I thought of encapsulating 1 of her and shamelessly including her in my own library ^^
The original old file is as follows: click to download

First, analyze 1 the deficiencies in this code (I think) :
1, process-oriented programming, if you want to modify the properties of the captcha, such as changing the font size, background color and other details, you need to go to 1 general processor to find the relevant code, modify it. This code is better, though, because it separates the generated captcha strings from the generated images in different ways, so it's easy to find changes.
2. If I want to apply this functionality to other applications, such as winform, the code should not be reused as a component (1 dll), but copied and pasted and modified.
3. It is not found that the verification code is saved after it is generated (--!).

The lack of so-called is relative, of course, is to have time to think about and then design a captcha class certainly can slowly to make a relatively good class, but if it is for a project and the project is very urgent, this code is to actually, the so-called object-oriented design pattern code decoupling what is floating clouds ~ ~ ~

The next step is to modify the code. First, I decide what effect I want to have, that is, how I want to use it after encapsulating it. First of all, I hope that when using only need new1 authentication code examples (needed for each attribute verification code can be arbitrarily set), can then call the instance of certain method on a string, flow, bitmap object, byte array to obtain the authentication code in the form of (description, the verification code does not include the verification code into the function of the context, personal feel to generate authentication code and deposit the authentication code to the context is two kinds of different functions, it is not necessary to put this function to do verification code class). In this way, the code becomes extremely simple in the 1 general handler. First, as long as new generates a captcha, and then calls the method to get the captcha in the form of a string, and then saves it in the last afternoon, as long as it is stored in Session or Cookie, the captcha class does not care about these. Then it calls another method to get the captcha in the form of an byte array, so that the image can be output through the context.Response.BinaryWrite () method, which means that there are only three lines of code in the 1 general handler. The code for the call is roughly as follows:
 
/// <summary> 
///  Get the captcha ( 1 General handler entry function)  
/// </summary> 
/// <param name="context"> Current context </param> 
public void ProcessRequest(HttpContext context) 
{ 
//  Create captcha  
ValidateCode validateCode = new ValidateCode(); 
//  Get the captcha (string) and write Session 
context.Session["SomeValidateCode"] = validateCode.GetString(); 
//  Output verification code (picture)  
context.Response.BinaryWrite(validateCode.GetByteArray()); 
} 

Here attached is the source code of the verification class (VS2010), which provides incomplete properties. In addition, the output of the picture is in the form of BMP, which is much clearer than the output of the original code. To a certain extent, it encapsulates the function, but not completely, for communication and learning: click to download new

PS: I remember when I first came to work, I wrote the code badly (worse than now). I just used the code directly when I got a copy, and I never thought about whether to modify or encapsulate the code. At that time, I was taught by my teacher once, which was very impressive. Personal feeling in fact there's nothing wrong with others code, 1 others used at least to prove the availability of the code, and intelligence is limited, there are some things you want to break the scalp and no others want to good, but the use of other people's code 1 don't blindly took use of the best according to their own actual situation to make the necessary modification or encapsulation, even the simple isolation layer 1. Of course, again, this is obviously a waste of time if the project is urgent, but don't just think about it, use it, or at least try to figure out the key code or the overall structure of the code.

The site is packaged directly. Above is the old folder, below is the contents of the new folder. Download address / 201012 / yuanma yanzhengma rar

Related articles: